React-Update Array->Objext State

2018, Dec 03    

If you have a array object your state like this;

constructor(props) {
    super(props);
    this.state = {
        data: [{id: 0, character: 'A'},
               {id: 1, character: 'B'}
        ]
}

You can’t update directly;

this.setState({ data[0][character]: C}]

If you want to update your state just one object, you can use react-addons-update.

Install react-addons-update

npm install react-addons-update

Import it

import update from 'react-addons-update'; // ES6

We can use now;

this.setState(
  {
    data: update(this.state.data, {
        0: {character: {$set: new1}},
    }),
  }
)

Immutability Helper