简体   繁体   English

属性在 Readonly { } 类型上不存在

[英]Property does not exist on type Readonly { }

I am getting the following error:我收到以下错误:

"Property 'items' does not exist on type 'Readonly<{}>'." “'Readonly<{}>' 类型不存在属性 'items'。” and "Property 'value' does not exist on type 'Readonly<{}>'."和“属性 'value' 在类型 'Readonly<{}>' 上不存在。”

I am trying to create "Add comment where you can upvote and downvote".我正在尝试创建“添加评论,您可以在其中投票和投票”。 Also when I refresh the page, the item which is added, is not available anymore.此外,当我刷新页面时,添加的项目不再可用。

The following code is using React with typescript:以下代码使用带有打字稿的 React:

Abc.tsx:


  export default class Abc extends React.Component{

constructor{

}
handleChange(event){

}

addItem(){

  })
  this.setState({items:itemsCopy, value:''});
}




 render(){
   return(
    <div>
      <input value={this.state.value} onChange={this.handleChange.bind(this)} />
      <button onClick = {() => this.addItem()}>Submit</button>
      <PostList postList = {this.state.items} 
                updateScore = {this.updateScore.bind(this)}
                removeItem = {this.removeItem.bind(this)}/>
    </div>
  );
  }
}

To answer the first part of your question, you will need to explicitly define the typings for your component's state.要回答问题的第一部分,您需要明确定义组件状态的类型。 You can do so by writing an interface for it.你可以通过为它编写一个接口来做到这一点。

interface AbcState {
  items: any[]; //replace any with suitable type
  value: string;
}

export default class Abc extends React.Component<{}, AbcState> {
 // the rest

}

As for the issue of data not being persisted, the local component state does not store any data beyond its lifecycle.至于数据不被持久化的问题,本地组件状态不会存储任何超出其生命周期的数据。 You can either save the data in your database, or cache it in your local storage.您可以将数据保存在数据库中,也可以将其缓存在本地存储中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型“ Readonly &lt;{}&gt;”上不存在属性“ xxx” - Property 'xxx' does not exist on type 'Readonly<{}>' 类型“Readonly&lt;{}&gt;”上不存在属性“posts” - Property 'posts' does not exist on type 'Readonly<{}>' 类型“只读&lt;{}&gt;”上不存在属性“值” - Property 'value' does not exist on type 'Readonly<{}>' 类型“只读&lt;{}&gt;”.ts 上不存在属性“vis” - Property 'vis' does not exist on type 'Readonly<{}>'.ts 属性“值”在只读类型上不存在 - Property “value” does not exist on type Readonly 类型“只读&lt;{}&gt;”上不存在属性“产品” - Property 'products' does not exist on type 'Readonly<{}>' 属性 &#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<{}>' 属性 'setUser' 不存在于类型'Readonly&lt;{}&gt; &amp; Readonly&lt;{ children?: ReactNode; }&gt;' - Property 'setUser' does not exist on type 'Readonly<{}> & Readonly<{ children?: ReactNode; }>' 错误TS2339:类型“ Readonly &lt;{}&gt;”上不存在属性“ items” - error TS2339: Property 'items' does not exist on type 'Readonly<{}>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM