Sub topic 3
Treat state as read-only: replace, don't change
This chapter assumes you know JavaScript objects, arrays, and the spread operator. It also assumes you can write a basic React function component with useState and handle events.
The rule
State can hold any JavaScript value, including objects and arrays. React still treats every value in state as read-only. To change what is on screen, you don't edit the value you have. You build a new value and pass it to the setter function.
Think of it as replacing a page in a book instead of erasing words on the old page. The old page, which is the previous snapshot, stays intact. React gets a new page to render.
Mutation versus replacement
A mutation changes an existing object in place. A replacement creates a different object that holds the new data.
import { useState } from 'react';
export default function Profile() {
const [person, setPerson] = useState({ name: 'Ada', score: 0 });
function addPointWrong() {
person.score = person.score + 1; // mutation
setPerson(person); // same object passed back
}
function addPointRight() {
setPerson({ ...person, score: person.score + 1 }); // replacement
}
return (
<div>
<p>{person.name}: {person.score}</p>
<button onClick={addPointWrong}>Wrong +1</button>
<button onClick={addPointRight}>Right +1</button>
</div>
);
}The "Wrong +1" button usually does nothing visible. The "Right +1" button works every time.
Why replacement is what React needs
When you call a setter, React compares the new value with the current one using Object.is. For objects and arrays, this checks whether both values are the same reference, meaning the same object in memory. It does not compare their contents.
In addPointWrong, you edit the object and pass the same reference back. React sees no difference and may skip the re-render. The data has changed, but the screen has not, so the two are out of sync.
In addPointRight, the spread expression creates a new object. The reference differs, so React re-renders.
Common mistakes
- Assigning to a property of a state object, such as
person.score = 5, and then calling the setter with the same object. - Calling
push,splice, orsorton a state array and passing that same array to the setter. - Believing that mutation works because it "sometimes" shows up. It may appear after an unrelated re-render, which makes the bug hard to trace.
Local variables are fine to mutate
The rule applies to values held in state. An object you create during the current render is not state yet, so you can change it freely before you pass it on.
function handleBonus() {
const next = { ...person }; // new object, created just now
next.score += 10; // safe: nobody else holds this object
setPerson(next);
}This is still replacement. You copied first, edited the copy, and set the copy. The next chapters give you shorter patterns for this.
Exercise
A component holds const [user, setUser] = useState({ name: 'Sam', age: 30 });. A click handler runs user.age = 31; setUser(user);. Why might the screen not update, and how do you fix it?
Show answer
The handler mutates the existing object and passes the same reference to the setter. React compares references with Object.is, sees no change, and may skip the re-render. Fix it by passing a new object: setUser({ ...user, age: 31 });.