Skip to content
Tutorials Center
My learning

Topics

React Reference ยท Concepts and Pitfalls

Stale closures

Why timers, listeners and effects in React sometimes read outdated state or props, how to recognize a stale closure, and four ways to fix it.

A stale closure is a function that still remembers variables from an older render. In React every render creates new functions that capture that render's props and state (see State as a snapshot). Usually React replaces old functions with new ones, so nothing goes wrong. The trouble starts when a function is handed to something that lives longer than one render and is not replaced: an interval, a subscription, a native event listener or an effect that does not re-run.

The classic symptom: the UI shows the correct, current value, but the callback behaves as if it were still the initial one. A counter that never gets past 1, or a log that always prints the first value, almost always points to a stale closure.

JSX
useEffect(() => {
  const id = setInterval(() => setCount(count + 1), 1000); // captures count from the first render
  return () => clearInterval(id);
}, []);

Try it

This interval was created once with [] as dependencies, so its callback always sees count as 0. The counter jumps to 1 and stays there.

An interval stuck on an old value

Below are three fixes in one demo. The updater form does not need the current value at all, a ref always holds the newest value, and a dependency array makes the effect restart with fresh values.

Three ways to fix it

Syntax

A fix depends on what the long-lived function needs:

Situation Fix
It only computes the next state from the previous one Pass an updater to the setter: setCount((n) => n + 1).
It must run again whenever a value changes List the value in the effect's dependency array so the effect (and its cleanup) restart.
It must read the newest value but the effect should not restart Read it through a ref updated on every render, or use useEffectEvent (React 19.2+).
It is a handler passed to a native listener Re-register it when the handler changes, or store the handler in a ref and register a stable wrapper once.
JSX
const onTick = useEffectEvent(() => {
  console.log('latest count is', count);
});
useEffect(() => {
  const id = setInterval(() => onTick(), 1000);
  return () => clearInterval(id);
}, []);

Usage notes

  • The dependency array is the contract. If an effect uses a value from the component, list it. eslint-plugin-react-hooks warns when you do not.
  • Do not "fix" the warning by deleting the dependency. Silencing the lint rule is how stale closures reach production.
  • Restarting an interval each time count changes is correct but resets its phase. If that matters (a clock that must tick every second), prefer the updater form or useEffectEvent.
  • Async functions have the same problem. After an await, the code continues with the variables of the render that started it. A fetch that finishes late can use old props; guard with an ignore flag.
  • Closures in useCallback and useMemo go stale when their dependency list is incomplete.
  • Class components did not have this issue for this.state, which is mutable; the snapshot model in function components makes it explicit.
  • Refs trade safety for freshness. Changing a ref does not render, so use it only for values the callback reads, not values the UI shows.