I am using Redux. In my reducer I'm trying to remove a property from an object like this:
const state = {
a: '1',
b: '2',
c: {
x: '42',
y: '43'
},
}
And I want to have something like this without having to mutate the original state:
const newState = {
a: '1',
b: '2',
c: {
x: '42',
},
}
I tried:
let newState = Object.assign({}, state);
delete newState.c.y
but for some reasons, it deletes the property from both states.
Could help me to do that?
That's because you are copying the value of
state.c
to the other object. And that value is a pointer to another javascript object. So, both of those pointers are pointing to the same object.Try this:
You can also do a deep-copy of the object. See this question and you'll find what's best for you.
I use this pattern
but in book i saw another pattern