Can anyone please explain to me why and how this might happen?
I have a typescript app with Zustand state management.
Somewhere inside the app I am updating certain elements by extracting them from the state and cloning them via a simple Object.Assign :
let elemToUpdate = Object.assign({},story?.content?.elementsData[nodeId]);
if (elemToUpdate) {
if (elemToUpdate.title) elemToUpdate.title[editorLang] = newName;
else elemToUpdate.title = {[editorLang]:newName} as TextDictionary;
updateElement(nodeId,elemToUpdate);
}
Now the interesting part is: on my first try the update goes through without fail, but the next object I am trying to update fails with the following message:
Tree.tsx:39 Uncaught TypeError: Cannot assign to read only property 'en' of object '#<Object>'
I can't understand WHY the first one comes through, but the second gets blocked.
I know HOW to fix this: I need to do a deep clone. I just want to understand WHY.