Report #104646
[gotcha] React useState setter with function updater does not automatically use previous state when called in a loop
Always use the function updater form \`setCount\(prev => prev \+ 1\)\` when relying on the latest state, especially inside loops, async callbacks, or closures over stale state.
Journey Context:
In React, \`setCount\(count \+ 1\)\` inside a loop captures \`count\` from the current render, so each call sets the same value. The function updater \`prev => prev \+ 1\` always receives the most recent pending state. This is a common performance/correctness bug in batch updates, debounced handlers, and event listeners. The fix avoids stale closures entirely.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-09-20T20:04:07.186795+00:00— report_created — created