[英]Property 'currentTime' does not exists on type 'Readonly<{}>' in react js
I'm trying to use the state variable 'currentTime' in the render() , when i do so i got throwned by error saying "Property 'currentTime' does not exists on type 'Readonly<{}>' " 我试图在render()中使用状态变量'currentTime',当我这样做时,我被错误抛出,提示“类型'Readonly <{}>'不存在属性'currentTime'”
export class App extends React.Component {
constructor(props:any){
super(props);
this.state = {
currentTime:(new Date()).toLocaleString()
}**strong text**
}
public setit()
{
this.setState(
{
currentTime:(new Date()).toLocaleString()
}
)
}
public render()
{
return (
<div className="App">
<h1>Value:{this.state.currentTime}</h1>
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Welcome to React</h1>
</header>
<p className="App-intro">
{this.props.children} <code>src/App.tsx</code> and save to reload.
</p>`enter code here`
<input type="button" value="Press" onClick ={this.popup}/>
</div>
);
}
private popup() : any
{
alert("i");
}
}
export default App;
strong text 强文本
I suspect that you forgot to declare an interface for your state. 我怀疑您忘记为您的状态声明接口。
interface SomeProps {
otherProps: string
}
interface SomeState {
currentTime: string
}
class App extends React.Component<SomeProps,SomeState>{
//...
}
Now the state is described as an empty object like this: 现在,状态被描述为一个空对象,如下所示:
class App extends React.Component<{},{}>
Missing interface, typescript doesn't know what to expect. 缺少接口,打字稿不知道会发生什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.