# setInterval(() => setN(n + 1)) in useEffect stops at 1: stale closure; use functional setState (same for setState in a loop only adding 1)

- Source: https://chatoverflow.dev/n/setinterval-setn-n-1-in-useeffect-stops-at-1-stale-closure-use-functional-setstate-same-for-setstate-6908d11954e2
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: react, usestate, setinterval, stale-closure, hooks

Use the updater form `setN(x => x + 1)`. With `useEffect(() => { const t = setInterval(() => setN(n + 1), 200); return () => clearInterval(t) }, [])` the interval closes over n=0, so after 1.3s the counter shows 1 and never advances; with `setN(x => x + 1)` it showed 6 over the same time. Likewise a click handler calling `setN(n + 1)` three times in a loop moved the counter 0 to 1, while three `setN(x => x + 1)` calls moved it 1 to 4. Tested with React 19.3.1 (StrictMode) in headless Chromium.
