Chapter 5 of 8
Course Progress0 of 8 completed (0%)

Modern Full-Stack & React Internals · Chapter 5 · ~15 min · Advanced

Reconciliation, Fiber Architecture & The Diffing Algorithm

Journey into React's engine: Virtual DOM trees, React Fiber data structures (child, sibling, return pointers), 2-phase pipeline (Render vs Commit), and why array indices as keys cause destructive UI bugs.

  • Trace the 2-phase pipeline: Render (reconciliation) vs Commit (DOM mutation)
  • Inspect Fiber node anatomy (child, sibling, return, memoizedState)
  • Understand heuristic O(N) diffing rules across component element types
  • Diagnose why array index keys corrupt component state during reorders

Reconciliation, Fiber Architecture & The Diffing Algorithm: learn it visually

Journey into React's engine: Virtual DOM trees, React Fiber data structures (child, sibling, return pointers), 2-phase pipeline (Render vs Commit), and why array indices as keys cause destructive UI bugs.

Reconciliation, Fiber Architecture & The Diffing AlgorithmJourney into React's engine: Virtual DOM trees, React Fiber data structures (child, sibling, return pointers), 2-phase pipeline (Render vs Commit), and why array indices as keys cause destructive UI bugs.UNDERSTANDReconciliationFOLLOW THE FLOWFiber ArchitectureAPPLY ITDiffing AlgorithmReconciliation, Fiber Architecture & The Diffing AlgorithmJourney into React's engine: Virtual DOM trees, React Fiber data structures (child, sibling, return pointers), 2-phase pipeline (Render vs Commit), and why array indices as keys cause destructive UI bugs.UNDERSTANDReconciliationFOLLOW THE FLOWFiber ArchitectureAPPLY ITDiffing Algorithm

1The 2-Phase Pipeline: Render vs Commit

To understand React performance and debugging, you must distinguish between the two distinct phases React executes on every update:

Phase 1

The Render Phase (Reconciliation)

React executes your component functions, constructs a new tree of Fiber nodes, and diffs them against the existing tree. This phase is pure computation in JavaScript — no browser DOM nodes are touched! It is interruptible and can be paused or restarted by Concurrent React.

Phase 2

The Commit Phase (DOM Mutation)

React applies the calculated list of changes (the effect list: placements, updates, deletions) to the real browser DOM synchronously. After the DOM is painted, React runs useLayoutEffect and then schedules useEffect.

Wrong idea: Complete DOM re-creation on every state change

container.innerHTML = "";
container.appendChild(newEntireDOMTree);
Destroys DOM nodes · loses input focus & scroll positions

Assuming React destroys the existing DOM sub-tree and recreates all browser elements from scratch whenever anything changes.

Real system: Heuristic O(N) diffing algorithm across Fiber nodes

FiberDiff(oldFiber, newJSX) → PatchDOM(onlyChangedProps)
Preserves DOM nodes · surgical in-place attribute mutation

React compares nodes at the same tree depth. Different tag types trigger unmount/remount; same tag types update only changed attributes and preserve DOM nodes.

2Inside a Fiber Node: Singly-Linked Work Units

Before React 16, React used a Stack Reconciler: it walked the virtual DOM tree recursively using the JavaScript call stack. If your tree had 2,000 components, JavaScript would monopolize the main thread for 40ms, dropping animation frames and causing sluggish typing.

React Fiber completely redesigned this. A Fiber is a JavaScript object that represents a single unit of work. Instead of relying on the call stack, Fibers are linked together in a singly-linked list with three key pointers:

interface FiberNode {
  // 1. Tag & Type:
  tag: WorkTag;            // FunctionComponent, HostComponent ('div'), etc.
  type: any;                // Component function or tag string

  // 2. Singly-Linked Tree Pointers:
  child: FiberNode | null;   // First direct child
  sibling: FiberNode | null; // Next sibling at same depth
  return: FiberNode | null;  // Parent Fiber (return target)

  // 3. State & Props:
  memoizedProps: any;       // Props used in the previous render
  memoizedState: any;       // Hook linked list (useState, useReducer)

  // 4. Mutation Flags:
  flags: Flags;             // Placement, Update, Deletion, Passive
}

Because React holds explicit child, sibling, and return pointers, it can pause its traversal in the middle of a loop, let the browser handle a user mouse click or keystroke, and then resume exactly where it left off!

3The Heuristic O(N) Diffing Rules

Finding the minimum number of operations to transform one arbitrary tree into another has a theoretical algorithmic complexity of O(N³). For a tree of 1,000 elements, that would require one billion comparisons!

React reduces this to a lightning-fast O(N) algorithm based on two practical heuristics:

  1. Two elements of different types produce different trees: If a <div> becomes a <span>, or <Header /> becomes <Footer />, React tears down the old tree completely (destroying state) and mounts the new one from scratch.
  2. Developers can hint at element identity across renders using the key prop:When comparing lists of children, keys allow React to match an existing child even if its position in the array changed.

Predict first

What happens to the internal input state if an element changes from <input className='box' /> to <textarea className='box' />?

4Keys Demystified: The Array Index Trap

Why does ESLint yell at you when you write items.map((item, index) => <Item key={index} />)?

When you use array indices as keys:

  • Item at index 0 always has key="0".
  • Item at index 1 always has key="1".

If you prepend a new item to the beginning of the list, the new item becomes index 0! React compares the previous Fiber with key="0" against the new element with key="0". React thinks: "Aha! The component at key='0' didn't move, only its title prop changed!"

React updates the text label, but reuses the existing DOM element and its internal state (like checkboxes, text inputs, or CSS animations). Your user's typed note remains attached to row 0, even though row 0 is now an entirely different item!

5Hands-On: Fiber Tree Diff & Key Swap Visualizer

Inspect real Fiber nodes in memory. Toggle between array index keys and unique stable IDs, then prepend tasks to observe how the Fiber diffing engine re-links siblings and handles mutation flags:

Interactive Fiber Tree Inspector & Key Diffing Engine

Observe how Fiber reconciles list items when a new item is added to the top.
Fibers in Memory: 5
Fiber Tree Linked List Hierarchy:Click any node to inspect
<App /> (Root Fiber)child → TaskList
<TaskList />child → <ul>
<ul>child → id-1
<li> 1. Clean CSS tokens
key=id-1
sibling →
<li> 2. Add Vitest suite
key=id-2
sibling: null
Selected Fiber Node Anatomy:
App

Fiber nodes are plain JS objects representing units of work in React's cooperative scheduler:

tag / type:FunctionComponent
key:null
child pointer:TaskList
sibling pointer:null (last sibling)
return (parent):HostRoot
memoizedState:items.length = 2
effectTag / flags:
None
✓ Unique ID Advantage: React identifies that an entirely new Fiber was inserted at index 0 and existing Fibers simply shifted down, preserving all input state!

6Bug Hunt: Fix the Corrupted State Index Key Bug

A customer dashboard has a task list where prepending an urgent item causes notes entered into text inputs to shift to the wrong row. Diagnose and fix the array index key bug below:

Destructive Array Index Key Bug Hunt
TypeScript JSX

🎯 Find and fix the bugs in the code above, then press Run & verify to test your solution.

Need a hint?
  • Replace key={index} with key={task.id}.
  • Keep unique identifiers persistent across list reorders.

7Chapter Knowledge Challenge

Confirm your understanding of Fiber data structures, reconciliation, and keys:

Concept Check 1 of 3
1

In the React Fiber singly-linked list, what does the 'return' pointer represent?

Concept Check 2 of 3
2

What diffing action does React take when an element's tag type changes from <div> to <section> at the same tree location?

Concept Check 3 of 3
3

Why does prepending an element to a list with key={index} corrupt input form fields?

8Recap & Architectural Takeaways

  • The 2 Phases: Render phase is pure calculation and interruptible; Commit phase mutates the DOM synchronously.
  • Fiber Anatomy: Linked list of work units (child, sibling, return) enabling cooperative scheduling.
  • O(N) Diffing Heuristics: Comparing tag types at the same tree depth avoids O(N³) tree matching complexity.
  • Keys Are Identities: Always use stable business IDs for list keys, never array indices.

Ready for Data Fetching & Race Conditions in Chapter 6?

Enjoying this free lesson? Your support helps us build more free animated chapters.

Support Us