简体   繁体   English

React JS中类型'Readonly <{}>'上不存在属性'currentTime'

[英]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.

相关问题 &#39;Readonly&lt;{}&gt; &amp; Readonly&lt;{ children?: ReactNode; 类型上不存在属性“导航” }&gt; - Property 'navigation' does not exists on type 'Readonly<{}> & Readonly<{ children?: ReactNode; }> 属性 &#39;google&#39; 不存在于类型 &#39;Readonly&lt;{ IGoogleMapTrackerProps }&gt; &amp; Readonly&lt;{ chirldren?: ReactNode; }&gt;&#39; - Property 'google' does not exist on type 'Readonly<{ IGoogleMapTrackerProps }> & Readonly<{ chirldren?: ReactNode; }>' 属性 &#39;XYZ&#39; 不存在于类型 &#39;Readonly&lt;{ children?: ReactNode; }&gt; 和只读&lt;{}&gt;&#39; - Property 'XYZ' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>' 流星/反应-“ readOnly”属性的作用是什么? - meteor / react - What does the “readOnly” property do? 类型“ Readonly &lt;{}&gt;”上不存在属性“ xxx” - Property 'xxx' does not exist on type 'Readonly<{}>' 类型“只读&lt;{}&gt;”上不存在属性“产品” - Property 'products' does not exist on type 'Readonly<{}>' React Class 组件属性中的 TypeScript 错误在类型“Readonly&lt;{}&gt;”上不存在,不确定如何为 state 设置类型 - TypeScript errors in React Class Component property does not exist on type 'Readonly<{}>', not sure how to set types for state 在 React.JS 中,如何在渲染字段的组件上设置 readOnly 属性 - In React.JS how does one set the readOnly property on a component that renders a field 类型错误:无法读取未定义的属性currentTime - Type error: Cannot read property currentTime of undefined 类型“IntrinsicAttributes 和 IntrinsicClassAttributes”上不存在属性“名称”<product> &amp; 只读&lt;{}&gt;'</product> - Property 'name' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Product> & Readonly<{}>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM