You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I created a simple todo app, with 3 simple functions: addTodo, copyTodo, changeSecondTodoValue. "copyTodo" function makes a copy of an existing todo, changes id and pushes it at the and of array.
As I can see after dispatching "copyTodo" the property "obj" of both todos reference the same object what often causes problem of unintentional editing values in more than one place. However editing value with "changeSecondTodoValue" function using RTK approach doesn't make problem in this case. I know the reference changes because of how immer works behind the scenes, I know the mechanism.
I wonder if there can be scenario where it causes problem or it's not necessary and I shouldn't care as long as I follow RTK immer approach. Or maybe it would be better to copyTodo with cloneDeep from lodash library like that:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi,
I created a simple todo app, with 3 simple functions: addTodo, copyTodo, changeSecondTodoValue. "copyTodo" function makes a copy of an existing todo, changes id and pushes it at the and of array.
code looks like this:
In my component I dispatch addTodo then copyTodo and finally changeSecondTodoValue.
I was wondering how immer handles references of nested objects so I added this code in my component:
And here are logs:
As I can see after dispatching "copyTodo" the property "obj" of both todos reference the same object what often causes problem of unintentional editing values in more than one place. However editing value with "changeSecondTodoValue" function using RTK approach doesn't make problem in this case. I know the reference changes because of how immer works behind the scenes, I know the mechanism.
I wonder if there can be scenario where it causes problem or it's not necessary and I shouldn't care as long as I follow RTK immer approach. Or maybe it would be better to copyTodo with cloneDeep from lodash library like that:
Beta Was this translation helpful? Give feedback.
All reactions