State & ArchitectureBaseline · Widely

Stale Closures in Hooks & Callbacks

Mental Model

Every component re-render creates a brand-new JavaScript lexical environment with its own frozen props and state constants. When a setInterval, addEventListener, or useEffect(..., []) skips re-subscribing, its callback keeps reading the ghost of Render #1 forever.

Interactive Simulator

Stale Closure Lifecycle Across Renders

Trace how a timer registered in Render #1 stays bound to Render #1's lexical environment while Render #2 creates new state.

Step 1 / 3: 1. Render #1 Creates Closure (count = 0)

useEffect(..., []) registers setInterval. Its callback captures LexicalEnv#1 where count === 0.

setInterval(() => setCount(count + 1), 1000);
// count = 0
Internal State / Memory
renderCount
1
uiCount
0
timerClosureCount
0

Under the Hood

A stale closure is standard ECMAScript lexical scoping colliding with Virtual DOM re-renders and Event Loop callbacks. Each render prints a dated Polaroid: a timer created in Render #1 carries that Polaroid even after Render #10 hangs a newer photo.

  1. Render #1 declares const count = 0 and passes () => setCount(count + 1) to setInterval.
  2. When the timer fires 1,000 ms later, V8 resolves count against Render #1's LexicalEnvironment (0), writing 1 every single tick.
  3. Passing a functional updater c => c + 1, reading a mutable .current ref, or switching to Fine-Grained Signals (which pass a live box rather than a frozen primitive) eliminates the snapshot drift.
JS
// Buggy: closes over Render #1's `count`
useEffect(() => {
  const id = setInterval(() => setCount(count + 1), 1000);
  return () => clearInterval(id);
}, []); // eslint-disable-next-line react-hooks/exhaustive-deps

// Fixed: functional updater reads latest state from the queue
useEffect(() => {
  const id = setInterval(() => setCount((c) => c + 1), 1000);
  return () => clearInterval(id);
}, []);

Problem Genealogy

  • 2013–2018 (Class this.state): Avoided stale closures because this was a single mutable object, at the cost of unpredictable mid-async this.state mutations.
  • 2019 (Hooks & exhaustive-deps): Embraced pure per-render closures, making async effects deterministic while surfacing stale-closure traps whenever [] lied about dependencies.
  • 2023+ (useEffectEvent & Signals): Separated non-reactive event reads from reactive effect lifecycles.

Production Pitfalls

Production Pitfall
  • Silencing react-hooks/exhaustive-deps: Adding // eslint-disable-next-line to force an effect to run once is the fastest way to ship stale auth tokens or outdated filters in WebSocket handlers.
  • Debounced callbacks capturing old props: Creating a debounced save() inside render without memoizing or reading a ref sends yesterday's form state to the API.
When Not to Use
  • When not to use useRef to bypass dependencies: Don't stuff reactive UI state into a ref just to silence a linter, if the effect should re-run when userId changes, keep userId in the dependency array, or move filter/navigation state to URL as State Store.

Connections & Logical Next Steps

Seen in Architecture Autopsies

Mechanical Comparisons