“FetchPeriod”类型上不存在属性“state”

Dyl*_*lan 12 typescript reactjs react-router react-props

我正在尝试按照本教程学习 ReactJS: 教程

我是编程语言的新手,所以我现在不知道该怎么做。

当我尝试添加“Fetchemployee.tsx”文件时,该this.state方法出现错误。

(TS) 类型“FetchPeriod”上不存在属性“state”

这是代码:

import * as React from 'react';
import { RouteComponentProps } from 'react-router';
import { Link, NavLink } from 'react-router-dom';

interface FetchPeriodDataState {
    periodList: PeriodData[];
    loading: boolean;
}

export class FetchPeriod extends React.Component<RouteComponentProps<{}>, FetchPeriodDataState> {
    constructor(props) {
        super(props);
    this.state = { periodList: [], loading: true };


    fetch('api/Period/Index')
        .then(response => response.json() as Promise<PeriodData[]>)
        .then(data => {
            this.setState({ periodList: data, loading: false });
        });

    // This binding is necessary to make "this" work in the callback  
    this.handleDelete = this.handleDelete.bind(this);
    this.handleEdit = this.handleEdit.bind(this);
}
Run Code Online (Sandbox Code Playgroud)

然后我有 PeriodData 类:

export class PeriodData {
PeriodId: number = 0;
Description: string = "";
PeriodOwner: string = "";
PeriodName: string = "";}
Run Code Online (Sandbox Code Playgroud)

this.statethis.setState方法是给在标题中的错误,我似乎无法找到一个解决。

Rom*_*man 20

问题的可能原因之一是缺少 React 类型定义因此您可以尝试安装它们:

npm install --save-dev @types/react @types/react-dom
Run Code Online (Sandbox Code Playgroud)

如果没有安装类型,TS 无法正确推断 JSX 元素所需的道具。