Skip to content
Tutorials Center
My learning

Topics

React Reference ยท Rules of React

Never mutate props or state

Why React props and state must be treated as read-only snapshots, what happens when you mutate them, and how to update objects and arrays immutably.

Props and state are snapshots of one render. React decides whether anything changed by comparing references with Object.is: if you call setItems(items) with the same array object, React concludes nothing changed and skips the update, even though you pushed a new element into it. Mutating a prop is worse; it changes data that belongs to the parent, which will not know about it.

The rule is simple: never change objects, arrays or Maps that came from props, state or context. Make a copy with the change applied and use that. Mutating something you created during the current render is fine, because nobody else holds it yet.

JSX
// Wrong: same array, so React sees no change
items.push(newItem);
setItems(items);

// Right: a new array
setItems([...items, newItem]);

Try it

Mutation is invisible, copying works
Updating nested objects and props

The second example freezes the object it passes on purpose: assigning to a frozen object throws a TypeError in strict-mode code. React's development builds freeze the props object itself (so props.user = x fails), but they do not freeze the objects inside it, so user.name = 'Hacked' on an ordinary object succeeds silently and corrupts the parent's data. Never rely on an exception; just do not write to props.

Syntax

Immutable update patterns:

Goal Pattern
Add to array [...list, item]
Remove list.filter((x) => x.id !== id)
Replace one list.map((x) => (x.id === id ? { ...x, done: true } : x))
Insert at index [...list.slice(0, i), item, ...list.slice(i)]
Sort / reverse list.toSorted(...), list.toReversed(), or [...list].sort(...)
Update object { ...obj, key: value }
Nested object { ...obj, inner: { ...obj.inner, key: value } }
Map / Set new Map(old).set(k, v), new Set(old).add(v)

Edge cases: spread copies are shallow, so the nested objects are shared with the old version; copy each level you change. structuredClone() makes a deep copy when a structure is too awkward to spread.

Usage notes

  • Mutating state can look like it works because some other state update causes a rerender, then the mutated data appears. That is luck, and it breaks memoization (memo, useMemo, effect dependencies) which compares references.
  • Never write to props. Treat them as read-only inputs; if a child needs to change data, call a callback from the parent.
  • Copy before sorting. Array.prototype.sort, reverse, splice, push, pop and shift all mutate; toSorted, toReversed, toSpliced, with, slice, concat and filter return new arrays.
  • Immer (useImmer) lets you write mutating-looking code that produces copies, if you add that library.
  • State setters with a function (setN((n) => n + 1)) must also return new values rather than modify n.
  • Refs are the exception. ref.current is meant to be mutated, outside rendering.