9

I am trying to refresh content in the table generated with this library react-table. However, for some reason, it doesn't work, even though I change the state of the parameter which I pass to the Component.

<ReactTable
   data={this.state.data}
   columns={this.state.headers}
/>

And the function which changes data:

  let data= this.state.data;
  for (var i = 0; i < data.length; i++) {
    data[i].name="TEST"
  }
  this.setState({data: data})

I can see that the data has changed but the content of the table stays the same.

1
  • As a suggestion, do not assign your state data into a variable like that. Use Object.assign or spread syntax: let data = [ ...this.state.data ] Also, do not change a key's value like that, since if you change with this way, your original object mutates, too. Use .map as suggested @Shishir Arora's answer. This applies to spread syntax since it makes a shallow copy. Commented Aug 19, 2018 at 22:43

1 Answer 1

12
let data= this.state.data;
const newData = data.map(d=>({...d, name:"Test"}));
this.setState({data: newData})

Use above code. Reason: React does not see mutation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.