Why state doesn't update in React — and what reference comparison has to do with it

A painfully familiar symptom. You add an item to an array, drop in a console.log — the item is there in the console. It's not on screen. React seems not to have noticed.
It genuinely didn't notice, and there's a precise reason. React compares old and new state by reference, not by contents. You changed what's inside the box and handed it the same box — so it said, honestly enough, "nothing changed, not re-rendering."
Let's go through the three causes behind this symptom. The first is by far the most common.
Cause 1. You're mutating the array instead of replacing it
Here's the classic non-working code:
function handleAdd(title) {
todos.push({ id: nextId++, title, done: false }); // ❌
setTodos(todos);
}
push modifies the same array. You're passing setTodos exactly the reference it already had.
React's docs put it plainly: React ignores your update if the next state is equal to the previous state, as determined by an Object.is comparison. You returned the same object, so the update was dropped.
How to check. Scan your handlers for anything that changes data in place: push, pop, splice, sort, reverse, and for objects, assigning to a field (todo.done = true). If that's sitting next to a setState, you've found it.
How to fix. Don't modify — create:
// Add
setTodos([...todos, { id: nextId++, title, done: false }]);
// Remove
setTodos(todos.filter((t) => t.id !== id));
// Change one item
setTodos(todos.map((t) => (t.id === id ? { ...t, done: true } : t)));
Simple rule: map, filter and the ... spread are fine; push, splice, sort are not. The first group returns a new array, the second edits the old one. If you really need sort, copy first: [...todos].sort(...).
Cause 2. State is a snapshot, not a live variable
Second scenario: no mutation, everything is replaced properly, but the value lags by one step.
function handleClick() {
setCount(count + 1);
console.log(count); // the old value!
}
That's not a bug. Within a single render, count is a constant — a snapshot taken when the component rendered. setCount doesn't change it; it schedules the next render with a new value.
Which leads to a nastier version of the same mistake:
setCount(count + 1);
setCount(count + 1); // ❌ gives +1, not +2
Both calls read the same stale count. The second simply overwrites the first.
How to check. Are you computing the new value from the old one — twice in one handler, or inside an async callback? Then this is your case.
How to fix. Pass a function to the setter; it receives the value as of the moment it's applied:
setCount((c) => c + 1);
setCount((c) => c + 1); // ✅ now +2
The same trick works for arrays: setTodos((prev) => [...prev, newTodo]).
Cause 3. The spread only copied the top level
The most frustrating of the three, because it looks correct:
setUser({ ...user, address: user.address }); // ❌ address is the same object
Spread makes a shallow copy. The top level is a new object, but everything nested keeps the same references. Change user.address.city and you've changed it in the old state too.
How to check. Do you have an object inside an object, or an array of objects, in state? Are you copying only the outer level? The symptom is distinctive: part of the UI updates and the nested part doesn't.
How to fix. Copy every level you're changing:
setUser({
...user,
address: { ...user.address, city: "Almaty" },
});
And a forward-looking tip: the flatter your state, the fewer of these bugs. If you find yourself writing three nested spreads, that's a signal the structure wants simplifying.
Quick diagnosis
In order, thirty seconds:
- Any
push/splice/sort/ field assignment near the setter? → cause 1. - Computing a new value from the old one twice in one handler? → cause 2.
- Nested objects with a top-level-only spread? → cause 3.
- None of the above? Check you're looking at the right component — sometimes state lives higher up the tree while you're waiting for a re-render below. React DevTools helps: select the component and inspect its current state directly.
Why doesn't React just compare contents?
Speed. Deep-comparing large objects on every update would be expensive, and it would happen constantly. Comparing references is a single operation. The price of that speed is having to return new objects — which is exactly the "state is immutable" principle every React guide talks about.
Nothing updated — maybe it isn't the state?
Happens. If nothing renders at all and there's an error in the console, the component probably crashed during render, and that's a different story: why your page is blank and white. But if React DevTools shows fresh data while the DOM shows stale data — that's exactly the symptom described above.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





