Update nested state
Every level of nesting must by copied and updated appropriately
Mistake 1: New reference directly mutate original state
1 | function updateNestedState(state, action) { |
Mistake 2: Only making a shallow copy of one level
1 | function updateNestedState(state, action) { |
Doing a shallow copy of the top level is not sufficient - the nestedState
object should be copied as well.
Correct: Copy all level of nested data
1 | function updateVeryNestedField(state, action) { |
Thus we should:
- keep your state flattened
- compose reducers as much as possible.
Updating array
As long as we make a copy first, we can safely mutate the copy
1 | function insertItem(array, action) { |
Updating an item in the array:
1 | function updateObjectInArray(array, action) { |