Skip to content

React Reconciliation Interview Guide

Reconciliation is the process React uses to compare the previous Virtual DOM with the new Virtual DOM and determine the minimum number of changes required to update the real DOM.

flowchart TD
    A[State or Props Change] --> B[Create New Virtual DOM]
    B --> C[Compare with Previous Virtual DOM]
    C --> D[Diffing Algorithm]
    D --> E[Update Only Changed Real DOM Nodes]
  • Updates only changed nodes
  • Improves rendering performance
  • Reduces browser repaint and reflow
  • Avoids full DOM replacement

Updating the entire DOM is expensive. Reconciliation allows React to:

  • Identify what changed
  • Update only affected nodes
  • Improve rendering performance
  • Reduce browser repaint and reflow operations

Relationship Between Virtual DOM and Reconciliation

Section titled “Relationship Between Virtual DOM and Reconciliation”
  • Virtual DOM is the lightweight JavaScript representation of the UI.
  • Reconciliation is the algorithm that compares two Virtual DOM trees.
Virtual DOM -> Reconciliation -> Real DOM Update

The Diffing Algorithm compares old and new Virtual DOM trees efficiently.

Instead of comparing every node (O(n^3)), React uses heuristics to achieve approximately O(n).

Assumption 1: Different element types produce different trees

Section titled “Assumption 1: Different element types produce different trees”
<div>Hello</div>

changes to

<span>Hello</span>

React destroys the old tree and creates a new one.

Assumption 2: Developers provide stable keys

Section titled “Assumption 2: Developers provide stable keys”
{users.map(user => (
<User key={user.id} />
))}

Keys help React identify which items changed.


<div>
<Counter />
</div>

changes to

<span>
<Counter />
</span>

React:

  1. Destroys the old DOM subtree
  2. Unmounts components
  3. Creates a new subtree
  4. Mounts new components

State is lost.

<div className="old" />

changes to

<div className="new" />

React updates only the changed attribute:

<div class="new"></div>

The DOM node is reused.

<User age={20} />

changes to

<User age={25} />

React:

  1. Reuses the component instance
  2. Updates props
  3. Re-renders the component
  4. Reconciles child elements

Component state is preserved.


Keys uniquely identify list items.

Without keys:

users.map(user => <User user={user} />)

With keys:

users.map(user => (
<User key={user.id} user={user} />
))

Keys allow React to efficiently track additions, removals, and moves.

users.map((user, index) => (
<User key={index} />
))

Problems:

  • Incorrect state preservation
  • Unnecessary re-renders
  • UI bugs during insert/delete/reorder

Example:

Before:

A B C

After inserting D at the beginning:

D A B C

React incorrectly assumes every item changed.

Old:

[A, B, C]

New:

[A, D, C]

React:

  • Reuses A
  • Replaces B with D
  • Reuses C
flowchart LR
    A1[A] --> A2[Reuse]
    B1[B] --> B2[Replace with D]
    C1[C] --> C2[Reuse]

State is preserved when:

  • Component type remains the same
  • Key remains the same
  • Position in the tree remains the same
<Counter />

Changing the key forces a remount:

<Counter key={Date.now()} />
<UserForm key={userId} />

When the key changes:

<UserForm key={newUserId} />

React:

  • Unmounts the old component
  • Mounts a new component
  • Resets component state

React.memo, useMemo, and useCallback reduce unnecessary re-renders and reconciliation work by stabilizing props and skipping unchanged component trees. See the React Performance Optimization Interview Guide for detailed usage, trade-offs, and senior-level debugging scenarios.


Rendering Reconciliation
Creates Virtual DOM Compares Virtual DOM trees
Happens when state/props change Happens after rendering
Produces UI representation Calculates minimal updates
Generates a new tree Updates the real DOM efficiently

Why doesn’t React use O(n^3) tree comparison?

Section titled “Why doesn’t React use O(n^3) tree comparison?”

A complete tree comparison is computationally expensive. React uses heuristic diffing based on:

  • Element type
  • Stable keys

This reduces complexity to approximately O(n).

Proper keys:

  • Reduce DOM mutations
  • Preserve component state
  • Improve list rendering
  • Minimize reconciliation work

Poor keys (indexes or random values) may trigger unnecessary re-renders.

React Fiber is the reconciliation engine introduced in React 16 that breaks rendering work into small, interruptible units so it can be prioritized and scheduled efficiently. See the React Interview Study Guide for its core benefits.

How does React decide to reuse or recreate a component?

Section titled “How does React decide to reuse or recreate a component?”

React checks:

  1. Component type
  2. Key

Same type and same key:

<User key="1" />

The component is reused.

Different key:

<User key="2" />

The old component is destroyed and a new one is created.

How do you optimize reconciliation in large React applications?

Section titled “How do you optimize reconciliation in large React applications?”
  • Use stable keys
  • Use React.memo
  • Use useMemo
  • Use useCallback
  • Avoid unnecessary state updates
  • Normalize state structure
  • Split large components
  • Virtualize long lists using libraries such as react-window or react-virtualized

Reconciliation is React’s process of comparing the previous and current Virtual DOM trees using a diffing algorithm to identify and apply only the minimal necessary updates to the real DOM, improving rendering performance.

  • Reconciliation compares Virtual DOM trees to minimize real DOM updates.
  • React’s diffing algorithm relies on element types and stable keys for near O(n) performance.
  • Stable keys preserve state and improve list rendering efficiency.
  • Changing a component’s key forces a remount and resets its state.
  • React.memo, useMemo, and useCallback help reduce unnecessary reconciliation work.