# Stale Closures in Hooks & Callbacks vs Signals and Fine-Grained Reactivity (Reactive Graph)

- **Canonical URL:** https://frontend.rocks/vs/stale-closures-hooks-vs-fine-grained-signals/
- **Relation:** Contrasts With
- **Last Verified:** 2026-11-15
- **Verdict:** Signals pass a stable box reference whose .get() always reads the current value without stale closures.

## Comparison Matrix

| Dimension | Stale Closures in Hooks & Callbacks | Signals and Fine-Grained Reactivity (Reactive Graph) |
| :--- | :--- | :--- |
| **Maturity** | Baseline · Widely | Experimental |
| **Domain** | State & Architecture | State & Architecture |
| **Mental Model** | Explains how JavaScript lexical closures snapshot primitive state per render, causing timers, listeners, and suppressed dependency arrays to read outdated values. | Reactive model built on a directed acyclic dependency graph (Signal, Computed, Effect) updating only subscribed DOM nodes without virtual tree diffing. |
| **Prerequisites** | Virtual DOM & Reconciliation (Each VDOM re-render invokes the component function anew, creating fresh lexical scopes.) | Event Loop, Macrotasks, and Microtask Queue (Reactive effect batching commonly relies on a microtask flush.) |
| **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. | When not to use Signals: For simple forms or mostly static pages, native DOM primitives avoid unnecessary abstraction overhead; for multi-step workflows with illegal state combinations, pair or replace raw signals with Finite State Machines. |

## Stale Closures in Hooks & Callbacks — Under the Hood
A stale closure is standard ECMAScript lexical scoping colliding with [Virtual DOM](/concepts/virtual-dom-reconciliation) re-renders and [Event Loop](/concepts/event-loop-tasks-microtasks) 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](/concepts/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);
}, []);
```

## Signals and Fine-Grained Reactivity (Reactive Graph) — Under the Hood
1. **Automatic Tracking**: When a `computed` or `effect` runs, it pushes its context onto a global tracking stack; each `signal.get()` adds a `signal -> observer` edge.
2. **Push-Pull Propagation**: Calling `signal.set(v)` marks descendants `Dirty` (Push) without eagerly recomputing lazy derived values, avoiding wasted [V8 Hidden Classes](/concepts/v8-hidden-classes-ic) allocations on unused branches.
3. **Lazy Evaluation (Pull)**: A `computed` only recomputes when read, and short-circuits downstream effects (often flushed on the [Event Loop](/concepts/event-loop-tasks-microtasks) microtask checkpoint) if its new value equals the previous one.

```js
const count = signal(1);
const doubled = computed(() => count.get() * 2);

effect(() => {
  // Re-runs ONLY when `doubled` actually changes
  badgeEl.textContent = String(doubled.get());
});
```

## Interactive Labs & Dissections
- [Stale Closures in Hooks & Callbacks Lab](https://frontend.rocks/concepts/stale-closures-hooks/#lab) ([Markdown](https://frontend.rocks/concepts/stale-closures-hooks.md))
- [Signals and Fine-Grained Reactivity (Reactive Graph) Lab](https://frontend.rocks/concepts/fine-grained-signals/#lab) ([Markdown](https://frontend.rocks/concepts/fine-grained-signals.md))

## Related Architecture Autopsies
- [Autopsy #02: Anatomy of a Memory Leak on an 8-Hour Dashboard](https://frontend.rocks/autopsies/eight-hour-spa-memory-leak/) ([Markdown](https://frontend.rocks/autopsies/eight-hour-spa-memory-leak.md)): SPA operations dashboard climbs from 120 MB to 2.4 GB RAM over an 8-hour shift and crashes with Aw, Snap!
- [Autopsy #13: The useEffect Dependency Loop That DDoSed Our Own API](https://frontend.rocks/autopsies/useeffect-dependency-loop-ddos/) ([Markdown](https://frontend.rocks/autopsies/useeffect-dependency-loop-ddos.md)): Changing a single URL filter triggers an infinite render loop, Maximum update depth exceeded, and a burst of 429 Too Many Requests responses.

## Primary Sources
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures
- https://react.dev/reference/react/useEffect
- https://react.dev/learn/separating-events-from-effects
- https://github.com/tc39/proposal-signals
