Skip to main content
React Bug Hunt Medium +20 XP

Stale Closure

The counter always shows 1, not the incremented value.

ReactFind the bug
1const [n, setN] = useState(0);
2const 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.

Related challenges