I was playing around in React Native and came across this error. I don't understand why the first snippet throws an error, while the second one runs fine.
This does not work:
let newContent = { ...oldContent }; // <-- oldContent is a 'const'
newContent.value = newValue; // <-- property at 'root level' can be modified
newContent.nested.a = b; // <-- throws 'You attempted to set the key with the value on an object that is meant to be immutable and has been frozen.'
But this works fine:
let newContent = { ...oldContent };
newContent.value = newValue;
newContent.nested = {
a: b,
}
The value of nested.a
is defined on oldContent
.
Is this expected behaviour or am I doing something wrong in the first snippet?