What does changing an object by reference mean, and why do we copy state?
Answer out loud or in your head first, then compare.
Answer
When I assign one object to two variables, both can point to the same object. A change made through one variable is then visible through the other. That behaves differently from copying a plain number.
In React, instead of changing an existing state object, I usually create a new one, e.g. {...user, name: 'Ola'}. That makes the change explicit and I don't modify data used by earlier renders. The spread operator makes a shallow copy: a nested object can still be shared. If I change a nested field, I copy that level too. But there's no need to blindly deep-copy everything.
Why it matters: The basis of correct React state and of avoiding nasty data bugs.
Possible follow-ups: Does {...object} copy all the nested objects?
How did it go? Be honest — “Not yet” puts the question on your review list.
Related questions
- Shallow copy vs deep copy — what does spread copy? — JavaScript
Further reading
- MDN: Closures(opens in a new tab)Accessed 22.10.2026. Closures and a function's access to its surrounding scope.