To update array state in React, never change the array in place. Create a new array with [...items, newItem] to add, filter() to remove and map() to update, then pass that new array to the state setter. React compares state by reference, so only a new array tells it to re-render.
This guide uses React 19 with function components and useState. The same patterns apply to React 18 and earlier, and they also work for arrays of objects, which is the most common real case.
useState stores a reference to the array. If you call items.push(x) and then setItems(items), the reference is the same, React sees no change (it compares with Object.is) and skips the re-render. Even when a re-render happens for another reason, mutation breaks memoisation, useEffect dependency checks and React's development double-render checks.
The rule is simple: treat state as read-only and always hand React a fresh array.
Use the spread operator to copy the old items into a new array and place the new one where you want it.
To insert at the start, write [newItem, ...prev]. To insert at index i, use [...prev.slice(0, i), newItem, ...prev.slice(i)]. The updater form setTodos((prev) => ...) is safer than reading todos directly when several updates can happen before the next render.
filter() returns a new array that keeps only the items for which the callback returns true. Filtering by a stable id is more reliable than filtering by index.
Do not use splice() on state, because it mutates the original array. If you only have an index, use prev.filter((_, i) => i !== index).
To change one item, map() over the array and return a modified copy of the matching item while returning every other item unchanged.
The important part is { ...t, done: !t.done }. It copies the object, so you never write t.done = true on an object that is still part of the old state. Unchanged items keep their identity, which lets React.memo skip re-rendering them.
If you really need to replace by position, copy first with toSpliced or with, both available in modern browsers and Node.js 20+.
sort() and reverse() mutate the array they are called on. Copy first, or use their non-mutating versions toSorted() and toReversed().
Derived views such as a sorted list should usually be computed during render, not stored as a second state variable.
Spreading works one level at a time. For an array inside an object inside an array, every level on the path must be copied.
When nesting gets deep, the immer library (and use-immer) lets you write mutating-looking code inside a draft that produces an immutable result.
Keeping state flat is still the best fix for deep nesting.
| Goal | Immutable pattern |
|---|---|
| Append | [...prev, item] |
| Prepend | [item, ...prev] |
| Insert at index | [...prev.slice(0, i), item, ...prev.slice(i)] |
| Remove by id | prev.filter((x) => x.id !== id) |
| Update by id | prev.map((x) => x.id === id ? { ...x, ...changes } : x) |
| Replace at index | prev.with(i, value) |
| Sort | prev.toSorted(compareFn) |
| Clear | [] |
Avoid push, pop, shift, unshift, splice, sort, reverse and direct index assignment on state arrays.
push mutates the existing array, so the reference is unchanged and React sees nothing new. Create a copy with [...prev, item] and pass that to the setter.
Use a stable id whenever you can. Indexes shift after insertions, removals and sorting, so an update can hit the wrong item.
Copying an array is linear, which is fine for lists of hundreds or a few thousand items. For very large lists, virtualise rendering and consider a normalised structure or Immer.
A ref does not trigger a re-render when changed, so it is the wrong tool for data that appears on screen.
State arrays are read-only. Add with spread, remove with filter, update with map and a copied object, and sort a copy. Use the updater form setItems((prev) => ...), give every item a stable id, and reach for Immer only when nesting makes manual copying painful.