Readonly类型中不存在属性"value"

MVG*_*DER 9 typescript reactjs

我正在学习如何使用React作为前端和spring作为后端来开发应用程序.在开发示例应用程序时,我遇到了如下错误:

       `(26,28): Property "value" does not exist on type 'Readonly<{}>`
Run Code Online (Sandbox Code Playgroud)

此错误是从我的App.tsx文件生成的,该文件包含JSX中的React Components定义.具有状态"值"的类组件定义如下.

class App extends React.component{
        constructor(props:any) {
             super(props);
             this.state = {
             value:[],
             isLoading:false
          };
       }
        ...
25      render(){
26        const value = this.state.value;
27        const isLoading = this.state.isLoading;
          ... 
    }
   }//End of Class*
Run Code Online (Sandbox Code Playgroud)

我不明白什么是错的.有人可以帮助我从不同的角度看待它,以便能够以不同的方式解决这个问题吗?

小智 21

你为自己申报了一个界面state吗?

看看Hello React和TypeScript,它显示了使用React Components接口的示例.

我怀疑你错过了这样的东西:接口IMyComponentProps {someDefaultValue:string}

interface IMyComponentState {
    someValue: string
}

class App extends React.Component<IMyComponentProps, IMyComponentState> {
  // ...
}
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢,我要做的就是React.Component &lt;{},any&gt;而不是React.Component。但是从这篇文章中得到了这个主意...再次感谢。 (2认同)

小智 5

你可以试试这个吗?

class App extends React.component<{},any>{ //your code}
Run Code Online (Sandbox Code Playgroud)