Sub topic 4
Updating objects with the spread operator
This chapter assumes you know JavaScript objects and the spread operator, the basics of function components and useState, and how to handle events in React. It covers how to update object state by building a new object and passing it to the setter.
Replace, don't change
React treats state as a read-only snapshot. The screen re-renders when you call the setter with a value React sees as different. If you edit the existing object and pass it back, the setter receives the same reference, so React has nothing new to render.
const [person, setPerson] = useState({ firstName: 'Ada', lastName: 'Lovelace' });
function handleFirstNameChange(e) {
// Wrong: person.firstName = e.target.value; setPerson(person);
// The setter receives the same object, so React sees no change.
setPerson({ ...person, firstName: e.target.value });
}The spread syntax ...person copies every property of person into a new object literal. A property written after the spread overrides the copied one. Here the result has the old lastName and the new firstName, and it is a different object from the one in the previous snapshot.
The update pattern
Every object update follows the same steps:
- Start a new object literal.
- Spread the current object into it to keep the properties you are not changing.
- Write the changed properties after the spread so they override the copies.
Order matters. If you write { firstName: e.target.value, ...person }, the old firstName from person comes later and overwrites the new one.
Spread copies only one level
Spread makes a shallow copy. It copies the top-level properties. If a property holds another object, the copy holds a reference to that same inner object, not a duplicate of it.
So for nested data, you copy each object on the path to the value you want to change.
const [person, setPerson] = useState({
name: 'Ada',
address: { city: 'London', country: 'UK' },
});
function handleCityChange(e) {
setPerson({
...person,
address: { ...person.address, city: e.target.value },
});
}The outer spread copies name and replaces address. The inner spread copies country and replaces city. Only the objects along the changed path are new.
Common errors
| Mistake | What happens | Fix |
|---|---|---|
| `person.firstName = value; setPerson(person);` | Same reference, no re-render | `setPerson({ ...person, firstName: value })` |
| `{ firstName: value, ...person }` | The old value overwrites the new one | Put the changed property after the spread |
| `{ ...person, address: { city: value } }` | `country` is lost because the inner object was not spread | `address: { ...person.address, city: value }` |
| `person.address.city = value` before spreading | The old snapshot is changed too | Copy the inner object, then set the property |
Exercise
A component holds const [user, setUser] = useState({ name: 'Sam', settings: { theme: 'light', lang: 'en' } });. Write the setter call that changes only theme to 'dark', and explain why setUser({ ...user, settings: { theme: 'dark' } }) is wrong.
Show answer
Use setUser({ ...user, settings: { ...user.settings, theme: 'dark' } });. The wrong version replaces settings with an object that has only theme, so lang is lost. Spread is shallow, so the inner settings object must be copied with its own spread.