Sub topic 7
Which array methods mutate and which do not
This chapter assumes you know JavaScript arrays, the map, filter, and slice methods, the spread operator, and the basics of useState in a function component.
React compares the new state you pass to the setter with the current state. If you mutate an array and pass back the same reference, React sees no change. So before you update an array in state, you need to know which methods change the array in place and which return a new one.
Two groups of methods
A mutating method changes the array it is called on. A non-mutating method leaves the original alone and returns a new value.
| Task | Avoid (mutates) | Prefer (returns new array) |
|---|---|---|
| Add | `push`, `unshift` | `[...arr, item]`, `[item, ...arr]`, `concat` |
| Remove | `pop`, `shift`, `splice` | `filter`, `slice` |
| Replace | `arr[i] = value`, `splice` | `map` |
| Sort or reverse | `sort`, `reverse` | copy first, then `sort` or `reverse` |
Other methods that mutate are fill and copyWithin. Newer engines also provide toSorted, toReversed, toSpliced, and with, which return copies. Check your browser and build targets before relying on them.
Why the distinction matters
Assigning to an index, such as arr[0] = 'x', also mutates the array. It is easy to miss because it looks like a plain assignment.
import { useState } from 'react';
export default function List() {
const [items, setItems] = useState(['a', 'b', 'c']);
function brokenAdd() {
items.push('d'); // mutates the array in state
setItems(items); // same reference, so React sees no change
}
function correctAdd() {
setItems([...items, 'd']); // new array, new reference
}
return (
<div>
<p>{items.join(', ')}</p>
<button onClick={brokenAdd}>Broken add</button>
<button onClick={correctAdd}>Correct add</button>
</div>
);
}Clicking "Broken add" changes the array's contents but does not trigger a re-render. The new item may appear later, when something else causes a render. That delayed, inconsistent behavior makes this bug hard to trace.
A common trap: `slice` and `splice`
These two names differ by one letter but behave in opposite ways. slice(start, end) copies a portion and leaves the original unchanged. splice(start, count, ...items) removes or inserts items in the original array and returns the removed items.
const letters = ['a', 'b', 'c', 'd'];
const copy = letters.slice(1, 3);
console.log(copy); // ['b', 'c']
console.log(letters); // ['a', 'b', 'c', 'd'] (unchanged)
const removed = letters.splice(1, 2);
console.log(removed); // ['b', 'c']
console.log(letters); // ['a', 'd'] (changed)Copying makes mutating methods safe
You can still use a mutating method if you call it on a fresh copy. The mutation then affects only your copy, not the array in state.
function reverseItems() {
const next = [...items];
next.reverse();
setItems(next);
}The copy is shallow. The objects inside it are the same objects as in the original, so changing one of their properties still mutates state. Later chapters cover that case.
Exercise
A component keeps const [tags, setTags] = useState(['react', 'state']). A teammate writes tags.sort(); setTags(tags); to sort alphabetically, and the list does not update reliably. Explain why, and write a fix.
Show answer
sort mutates the array in place, so setTags receives the same reference and React sees no change. Sort a copy instead: const next = [...tags]; next.sort(); setTags(next);.