Sub topic 9
Updating Objects in State
This chapter assumes you know how useState works, how event handlers call setters, and that each render sees a state snapshot. You should also be comfortable with spread syntax and computed property names in JavaScript.
Why mutation fails
React decides whether to re-render by comparing the new state value to the old one with Object.is. If you change a property on the existing object and pass the same object to the setter, the reference is identical. React sees no change and may skip the render.
// Wrong: mutates the existing object
person.name = 'Ada';
setPerson(person);Even when a render happens for another reason, mutation breaks snapshots. Earlier renders and pending handlers now see altered data.
The rule is to treat state objects as read-only. To change one, build a new object and pass it to the setter.
Copying with spread
Spread syntax copies the properties of an object into a new one. Properties listed after the spread override the copied ones.
import { useState } from 'react';
function ProfileForm() {
const [person, setPerson] = useState({ name: '', email: '' });
function handleChange(e) {
setPerson({ ...person, [e.target.name]: e.target.value });
}
return (
<form>
<input name="name" value={person.name} onChange={handleChange} />
<input name="email" value={person.email} onChange={handleChange} />
</form>
);
}The expression [e.target.name] is a computed property name. It uses the input's name attribute as the key, so one handler serves both fields. Unlike class component setState, the useState setter replaces the whole value. It does not merge. If you wrote setPerson({ name: 'Ada' }), the email property would be gone.
Nested objects
Spread is shallow. It copies only the top level, so nested objects are still shared with the old object. Copy each level you change.
import { useState } from 'react';
function AddressForm() {
const [person, setPerson] = useState({
name: 'Ada',
address: { city: 'London', zip: 'N1' },
});
function handleCityChange(e) {
setPerson({
...person,
address: { ...person.address, city: e.target.value },
});
}
return <input value={person.address.city} onChange={handleCityChange} />;
}If the nesting gets deep, consider flattening the state shape, which a later chapter covers.
Common errors
| Mistake | Result | Fix |
|---|---|---|
| `person.name = x; setPerson(person)` | No re-render, or corrupted snapshots | Pass a new object |
| `setPerson({ name: x })` | Other properties are lost | Spread `...person` first |
| Spreading before nested change only | Nested object is shared | Spread each changed level |
| Spread placed after the new value | Old value overwrites the new one | Put `...person` first |
When the new object depends on the previous one and several updates may queue in one handler, use an updater function: setPerson(p => ({ ...p, name: 'Ada' })).
Exercise
A component keeps { name: 'Ada', address: { city: 'London' } } in state. Write the setter call that changes only the city to "Paris" without mutating anything.
Show answer
setPerson({ ...person, address: { ...person.address, city: 'Paris' } }). The outer spread keeps name, and the inner spread keeps any other address fields.