React Bug Hunt Medium +20 XP
Stale Closure
The counter always shows 1, not the incremented value.
ReactFind the bug
1
const [n, setN] = useState(0);2
const inc = () => setN(n + 1); The bug is on line 2
Select the correct fix
Show answer and explanation
Answer: Use setN(prev => prev + 1)
When updating state based on previous state, use the functional updater form to avoid stale closures.
Hint: State updates may be batched.
What this challenge tests
Bug Hunt in React: hooks, closures.