Why is it considered approved to mutate a shallow copy of my condition?

Consider the following:

    [SELECT]: (state, action) => {
      let newState = {...state, newState} 
      delete newState[action.payload.key]
      return(newState)
}

Why is it acceptable for me to mutate a shallow copy, return it and still satisfy the rule not to mutate my condition?

+4
source share
2 answers

This is acceptable because (at least in your code example) the mutation is on a small object, so you are not modifying the object that any other code currently refers to.

It would be unacceptable to make a shallow copy and then modify the nested object! The key is that you clone any and all objects in the object tree that are on the path to the deep property that you want to change.

Redux:

, , , - . state.a.b.c.d, d, c, b, a state. , .

+5

Redux , , , ( ). , .

, , ( , ?). , , , , .

+2

Source: https://habr.com/ru/post/1652039/


All Articles