Reactjs 财产';州';不存在于类型';取数周期';
我将按照本教程学习ReactJS: 我是编程语言的新手,所以我现在不知道该怎么做 当我尝试添加“Fetchemployee.tsx”文件时,我在Reactjs 财产';州';不存在于类型';取数周期';,reactjs,typescript,react-router,react-props,Reactjs,Typescript,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 'rea
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);
}
this.state
和this.setState
方法在标题中给出了错误,我似乎找不到修复方法。问题的一个可能原因是缺少反应类型定义
因此,您可以尝试安装它们:
npm install --save-dev @types/react @types/react-dom
如果没有安装类型,TS无法正确推断JSX元素所需的道具。是否安装了React类型
@types/react
您确定构造函数中的状态初始化行给出了错误吗?贴出给出错误的那一行(和上下文),对不起,我在哪里可以检查这个?在Package.json中?是的,请确保您在dependencies
或devDependencies
中列出了@types/react
。您好,请参见下图:
npm install --save-dev @types/react @types/react-dom