How to update nested state properties in React How to update nested state properties in React reactjs reactjs

How to update nested state properties in React


In order to setState for a nested object you can follow the below approach as I think setState doesn't handle nested updates.

var someProperty = {...this.state.someProperty}someProperty.flag = true;this.setState({someProperty})

The idea is to create a dummy object perform operations on it and then replace the component's state with the updated object

Now, the spread operator creates only one level nested copy of the object. If your state is highly nested like:

this.state = {   someProperty: {      someOtherProperty: {          anotherProperty: {             flag: true          }          ..      }      ...   }   ...}

You could setState using spread operator at each level like

this.setState(prevState => ({    ...prevState,    someProperty: {        ...prevState.someProperty,        someOtherProperty: {            ...prevState.someProperty.someOtherProperty,             anotherProperty: {               ...prevState.someProperty.someOtherProperty.anotherProperty,               flag: false            }        }    }}))

However the above syntax get every ugly as the state becomes more and more nested and hence I recommend you to use immutability-helper package to update the state.

See this answer on how to update state with immutability-helper.


To write it in one line

this.setState({ someProperty: { ...this.state.someProperty, flag: false} });


Sometimes direct answers are not the best ones :)

Short version:

this code

this.state = {    someProperty: {        flag: true    }}

should be simplified as something like

this.state = {    somePropertyFlag: true}

Long version:

Currently you shouldn't want to work with nested state in React. Because React is not oriented to work with nested states and all solutions proposed here look as hacks. They don't use the framework but fight with it. They suggest to write not so clear code for doubtful purpose of grouping some properties. So they are very interesting as an answer to the challenge but practically useless.

Lets imagine the following state:

{    parent: {        child1: 'value 1',        child2: 'value 2',        ...        child100: 'value 100'    }}

What will happen if you change just a value of child1? React will not re-render the view because it uses shallow comparison and it will find that parent property didn't change. BTW mutating the state object directly is considered to be a bad practice in general.

So you need to re-create the whole parent object. But in this case we will meet another problem. React will think that all children have changed their values and will re-render all of them. Of course it is not good for performance.

It is still possible to solve that problem by writing some complicated logic in shouldComponentUpdate() but I would prefer to stop here and use simple solution from the short version.