1function Counter() {
2 const [count, setCount] = useState(0);
3
4 function tripleIncrement() {
5 setCount(count + 1);
6 setCount(count + 1);
7 setCount(count + 1);
8 // React batches these; all three set count to the same value
9 }
10
11 return <button onClick={tripleIncrement}>{count}</button>;
12}
no lines flagged
#052PracticeMedium15 min · 120 XP
Batched State Updates Miscounted
A counter incremented three times in a row only increases by one instead of three due to React batching.
Flagged linesNo lines flagged yet
What's wrong?
Flag a line or write a note to submit.