Full answer
Objects and arrays are compared by reference. If you write students.push(x) and then call setStudents(students), the reference is the same, so React treats the state as unchanged and skips the re-render. The data changed but nothing shows it.
The rule is to treat state as read-only and give React a fresh copy for every change. For objects, spread the old one and override a field: setUser({ ...user, name: 'Asha' }). For arrays, use methods that return new arrays. To add, [...list, item]. To remove, list.filter((x) => x.id !== id). To change one item, list.map((x) => x.id === id ? { ...x, done: true } : x). Avoid push, pop, splice, sort and reverse on state itself, since they mutate. Sort a copy: [...list].sort(...), or use toSorted.
Nested objects need a copy at each level you change: { ...state, address: { ...state.address, city: 'Pune' } }. Spread is shallow, so unchanged nested parts stay shared, which is fine and cheap.
Immutability gives practical benefits. memo and dependency arrays rely on changed references to detect change. Previous state stays intact, so undo features are easy. And bugs from one component mutating data another is reading disappear.
When nested updates get heavy, a library such as Immer lets you write mutating-looking code that produces an immutable result. Redux Toolkit uses it internally.
The example adds an item, flips a field and removes another, using only copies.
import { useState } from 'react';
export default function Students() {
const [students, setStudents] = useState([
{ id: 1, name: 'Asha', passed: true },
{ id: 2, name: 'Ravi', passed: false },
]);
function add() {
setStudents((list) => [...list, { id: list.length + 10, name: 'Meera', passed: true }]);
}
function toggle(id) {
setStudents((list) => list.map((s) => (s.id === id ? { ...s, passed: !s.passed } : s)));
}
function remove(id) {
setStudents((list) => list.filter((s) => s.id !== id));
}
return (
<div>
<button onClick={add}>Add Meera</button>
<ul>
{students.map((s) => (
<li key={s.id}>
{s.name}: {s.passed ? 'passed' : 'not passed'}
<button onClick={() => toggle(s.id)}>Toggle {s.name}</button>
<button onClick={() => remove(s.id)}>Remove {s.name}</button>
</li>
))}
</ul>
</div>
);
}Every update creates a new array and, where needed, a new object for the changed student. The check added Meera, toggled Ravi and removed Asha. Rendered result: <div><button>Add Meera</button><ul><li>Ravi: passed<button>Toggle Ravi</button><button>Remove Ravi</button></li><li>Meera: passed<button>Toggle Meera</button><button>Remove Meera</button></li></ul></div>
Key points
- React compares state by reference with Object.is, so mutated state with the same reference does not re-render.
- Use spread, map, filter and slice to build new objects and arrays.
- Copy every level you change in a nested structure.
- Sort or reverse a copy, never the state array.
- Immer can simplify deeply nested updates.
Common mistakes
- Calling push or splice on the state array and passing the same array to the setter.
- Using sort or reverse directly on state.
- Updating a nested field with a shallow spread and losing the rest of the nested object.
- Believing a deep clone of everything is needed. Copy only the path you change.
Follow-ups
- How does Immer let you write mutating code safely?
- Why does React's dependency comparison use Object.is?
- How would you update an item inside a nested array of objects?
Tips
- Start with the reference-equality reason. It explains why the rule exists.
- Know one operator for add, remove and update. Interviewers often ask live.
- Mention Immer as the practical shortcut for deep updates.