This click handler calls setCount(count + 1) three times:
function Counter() {
const [count, setCount] = useState(0);
function handleClick() {
setCount(count + 1);
setCount(count + 1);
setCount(count + 1);
}
return <button onClick={handleClick}>{count}</button>;
}
Click it once, and the counter shows 1, not 3.
It's not a React bug. It's how state works, and the same idea explains stuck timers and callbacks that read old values. I rendered every example below with React 19.3 (react-dom + jsdom) and recorded the actual results.
Video version:
Why: state is a snapshot
count isn't a live variable. It's a constant for the current render. Inside this click, count is 0 and stays 0:
setCount(0 + 1); // "set it to 1"
setCount(0 + 1); // "set it to 1"
setCount(0 + 1); // "set it to 1"
React batches the three updates and re-renders once, with 1.
Fix: updater functions
setCount(c => c + 1);
setCount(c => c + 1);
setCount(c => c + 1);
React queues updater functions and runs them in order, each receiving the latest pending state: 0 → 1 → 2 → 3. One click, 3.
Rule of thumb: if the next state depends on the previous state, pass a function.
Bug 2: "setState didn't work!"
function handleClick() {
setCount(count + 1);
console.log(count);
}
| Click | Logged | On screen |
|---|---|---|
| 1 | 0 |
1 |
| 2 | 1 |
2 |
The update worked. The new value just arrives in the next render. If you need it immediately, compute it first:
const next = count + 1;
setCount(next);
console.log(next);
Bug 3: the interval that stops at 1
useEffect(() => {
const id = setInterval(() => {
setSeconds(seconds + 1);
}, 1000);
return () => clearInterval(id);
}, []);
The effect runs once, so the interval callback captured seconds from the first render: 0, forever. Every tick sets it to 0 + 1. After ~5 seconds in my test, the screen still showed 1.
setSeconds(s => s + 1); // after ~5 s: 5
The updater doesn't read the captured variable, so it always works from the latest state. (Adding seconds to the dependency array also "works", but it tears down and recreates the interval on every tick.)
Bug 4: async callbacks see old values
function handleClick() {
setCount(count + 1);
setTimeout(() => {
report(count);
}, 1000);
}
Click three times quickly: the screen shows 3, but the three timeouts report 0, 1, 2. Each callback closed over the count from the render it was created in.
If a callback really needs the latest value, keep it in a ref:
const countRef = useRef(count);
useEffect(() => { countRef.current = count; }, [count]);
function handleClick() {
setCount(count + 1);
setTimeout(() => report(countRef.current), 1000);
}
Same three clicks: the timeouts report 3, 3, 3.
Cheat sheet
- State is a snapshot: constant for each render.
- New state depends on old? Use
setX(x => x + 1). -
console.logright aftersetXshows the old value (by design). - Effects and intervals capture values: use updaters.
- Async callbacks that need the latest value: use
useRef.
Which of these have you hit? 👇
I post one tested coding tip every day on YouTube at Refactorly. The 30-second version of this one is here.
Top comments (0)