How to update nested state properties in React

后端 未结 26 1976
野趣味
野趣味 2020-11-21 06:35

I\'m trying to organize my state by using nested property like this:

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

But updating

26条回答
  •  感情败类
    2020-11-21 07:28

    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.

提交回复
热议问题