What will count be after three setCount calls in a row?

Tests React state batching and stale closures. Answer: count is 1 because all three calls read the same closed-over value of 0. Fix: pass an updater function setCount(c => c + 1) three times so React queues each update.
Tests whether you understand React's automatic batching and that state values are immutable snapshots fixed for the duration of each render. Answer: count ends at 1 because the event handler closes over count=0, so every call computes 0+1. React batches the three identical updates and flushes them together in the next render. To increment by three, pass an updater function like prev => prev + 1 three times; React queues each updater and runs them sequentially against the pending state.
Read the original → react.dev
- #react
- #state
- #batching
- #hooks
- #closures
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.