To update object state in React without mutation, create a new object with the changes and pass it to the setter. The usual way is the spread operator: setUser({ ...user, name: "Ada" }) copies every property and then overwrites name. React sees a new object and re-renders.
You must avoid mutating object state, such as user.name = "Ada", because React decides whether to re-render by comparing the old and new values by reference. This guide covers flat objects, nested objects, arrays of objects and the tools that make updates shorter. It uses React 19.
State is a snapshot. When you write user.name = "Ada" and then call setUser(user), you pass React the same object reference it already has. React compares with Object.is, finds no change, and may skip the update. Even when the screen happens to refresh, mutation breaks features that rely on previous values, such as memoised components, useEffect dependencies and the updater function form.
The rule is simple: treat state as read-only and always set a new object.
{ ...prev, [name]: value } copies all properties of prev, then sets the one whose key matches the input's name. The square brackets create a computed property name, so one handler serves every field. The updater form (prev) => ... guarantees you copy the latest object.
Spread copies only one level. For a nested object, copy each level you change.
You copy prev, replace address with a copy of itself, and change city in that copy. Parts you did not touch, such as name, keep their references, which is fine because they are not changed.
For a list of objects, use map() to replace one item and filter() to remove one.
map() returns a new array. The matching item is replaced with a new object, and all other items are reused unchanged.
| Avoid (mutates) | Use instead (returns a new array) |
|---|---|
push, unshift | [...arr, item], [item, ...arr] |
pop, shift, splice | filter, slice, toSpliced |
arr[i] = x | map, or toSpliced(i, 1, x) or with(i, x) |
sort, reverse | toSorted, toReversed |
toSorted, toReversed, toSpliced and with are part of ES2023 and available in current browsers and Node.js.
Deeply nested spreading gets noisy. Two practical options:
use-immer package gives you useImmer, where you write code that looks like mutation inside a draft and Immer produces a new immutable object.Immer is optional. Spread is enough for most components.
Spread and Object.assign make shallow copies. structuredClone(obj) makes a deep copy but is rarely needed for state, and copying everything on each update is wasteful. Copy only the path you change.
It can appear to work when another update triggers a re-render, which is why the bug is easy to miss. It is unreliable and breaks memoisation, so do not do it.
No. Reuse their references. React only needs the changed path to be new.
useState calls?Use one object for fields that change together, such as a form. Use separate state variables for unrelated values, which makes updates simpler.
Object.assign the same as spread?For this purpose, yes, but Object.assign({}, prev, changes) is more verbose. Spread is the standard style.
Never change state objects in place. Create a new object with spread, copying each nested level you modify, and pass it to the setter, preferably in the updater form. Use map, filter and the non-mutating array methods for lists, flatten nested state when possible, and reach for Immer only when nesting becomes unmanageable.