Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property does not exist on type Readonly { }

I am getting the following error:

"Property 'items' does not exist on type 'Readonly<{}>'." and "Property 'value' does not exist on type '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:

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>
  );
  }
}
like image 396
Richa Avatar asked Jan 16 '20 18:01

Richa


1 Answers

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.

like image 63
wentjun Avatar answered Oct 19 '22 04:10

wentjun