How to update nested state properties in React

后端 未结 26 2018
野趣味
野趣味 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:25

    Two other options not mentioned yet:

    1. If you have deeply nested state, consider if you can restructure the child objects to sit at the root. This makes the data easier to update.
    2. There are many handy libraries available for handling immutable state listed in the Redux docs. I recommend Immer since it allows you to write code in a mutative manner but handles the necessary cloning behind the scenes. It also freezes the resulting object so you can't accidentally mutate it later.

提交回复
热议问题