07

React Internals

Fiber, reconciliation, hooks, stale closures

GoalStop treating React as magic. Understand reconciliation (it's the tree traversal you just learned), why hooks have rules, and what stale closures actually are.

Lessons

Render vs Commit

React updates happen in two phases:

Render phase — React calls your component functions, builds a new Virtual DOM tree, diffs it against the old one. Pure, no DOM writes, can be paused.

Commit phase — applies the minimal set of DOM changes. Runs useLayoutEffect, then useEffect. Synchronous.

"Re-render" means your function ran — not that the DOM was rebuilt. A component can re-render and produce zero DOM changes.

Reconciliation = tree diffing

React's diff algorithm is O(n) via two heuristics:

1. Different type → replace the whole subtree (don't diff children) 2. Same type → keep the node, update changed props → recurse into children

OLD: <div><p>Hi</p></div>
NEW: <div><span>Hi</span></div>
→ p and span are different types → p subtree torn down, span built fresh

Keys let React match list items by identity not position. Index-as-key breaks on insert/delete because the same index now maps to a different item.

How hooks actually work

React stores hook state in an ordered array per component, matched by call order. On every render, it reads slot 0, slot 1, slot 2...

js
function Form() {
  const [name, setName] = useState(""); // slot 0
  const [age, setAge] = useState(0);    // slot 1
  useEffect(() => {...});               // slot 2
}

If a hook is conditional, the slot order shifts — React reads the wrong state. This is why hooks must be unconditional and top-level.

Stale closures — the most important React bug

Each render creates a snapshot of state. Closures capture that snapshot.

js
useEffect(() => {
  const id = setInterval(() => {
    console.log(count); // ALWAYS logs initial value — stale closure
  }, 1000);
  return () => clearInterval(id);
}, []); // empty deps → effect never re-runs → always captures render-0's count

The interval callback was created on render 0, when count was 0. It captured render-0's count. New renders create NEW count values — but the old closure never sees them.

Fixes: add count to deps (re-run effect when count changes), use setCount(c => c + 1) (updater function bypasses captured value), or use useRef for always-current value.

Challenges2

Write working code. Run to see output. Check to grade.

CH1
Stale closure — predict the log
Hard

In this simulated stale closure scenario, what does the setTimeout log? Explain in a comment, then log the CORRECT predicted value.

js
// Simulates what happens in a React click handler with stale closure
let count = 0; // imagine this is a render-0 snapshot

function handleClick() {
  count = count + 1; // this creates a NEW count — doesn't mutate
  const capturedCount = 0; // the closure captured render-0's count
  setTimeout(() => {
    console.log("logged:", capturedCount); // what does this log?
  }, 100);
}
handleClick();
// Meanwhile, "state" has been updated to 1 (on screen)
JavaScript
CH2
Why the hook rule exists
Medium

React stores hooks in an ordered array. Explain what breaks when you conditionally call a hook.

Write code that simulates React's slot system with an array, and show what happens when a hook is conditionally skipped on the second render.

JavaScript

References