JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsreact
PrevNext

Learn the concept

React Internals

react
senior
internals

How does React's reconciliation algorithm work?

reconciliation
virtual-dom
diffing
fiber
performance
internals
Quick Answer

Reconciliation is React's diffing algorithm that compares the new virtual DOM tree with the previous one to determine minimal DOM updates. It uses heuristics: different element types trigger full subtree replacement, keys identify items in lists, and same-type elements update attributes.

Detailed Explanation

What is Reconciliation:

  • Process of comparing two virtual DOM trees
  • Determines what changed and needs updating
  • Aims to minimize actual DOM operations
  • Uses heuristics for O(n) performance vs O(n³) naive diff

Key Heuristics:

  1. Different element types:

    • Old tree is torn down completely
    • New tree is built from scratch
    • All child components unmount/remount
  2. Same element type:

    • Keeps same DOM node
    • Updates changed attributes only
    • Recursively reconciles children
  3. Keys for lists:

    • Identify items across renders
    • Enable efficient reordering
    • Prevent unnecessary unmount/remount

Fiber Architecture:

  • Incremental rendering
  • Can pause, abort, or reuse work
  • Assigns priority to different update types
  • Enables concurrent features (default rendering mode in React 19)

Code Examples

Different element typesJSX
// BEFORE
<div>
  <Counter />
</div>

// AFTER: Different root element type
<span>
  <Counter />
</span>

// React will:
// 1. Unmount old <div> and <Counter>
// 2. Counter's componentWillUnmount / useEffect cleanup runs
// 3. Mount new <span> and fresh <Counter>
// 4. All Counter state is LOST!

// Same element type - just updates attributes
<div className="before" title="old" />
<div className="after" title="new" />
// React updates className and title on same DOM node

// Same component type - instance is kept
<Counter count={1} />
<Counter count={2} />
// Same Counter instance, state preserved, just re-renders

Real-World Applications

Use Cases

Optimizing List Updates

Understanding reconciliation to structure list keys and component hierarchies for minimal DOM mutations

Animation Performance

Leveraging reconciliation knowledge to prevent layout thrashing during React-managed animations

Framework Debugging

Diagnosing unexpected re-mounts or state loss caused by reconciliation misunderstandings

Mini Projects

Reconciliation Visualizer

advanced

Build a tool that shows the Fiber tree diff process when state changes, highlighting which nodes are created, updated, or deleted

Key Strategy Benchmark

intermediate

Create a benchmark comparing reconciliation performance with different key strategies (index, UUID, stable ID)

Industry Examples

React DevTools

Uses Fiber tree inspection to show component render timing and commit phases

Million.js

Alternative virtual DOM that plugs into React with a faster reconciliation algorithm

Resources

React Docs - Preserving and Resetting State

docs

React Fiber Architecture

article

Related Questions

Explain React.memo, useMemo, and useCallback. When should each be used?

senior
performance

What are React's concurrent features and how do Suspense and transitions work?

senior
concurrent
Previous
Explain React.memo, useMemo, and useCallback. When should each be used?
Next
What are React's concurrent features and how do Suspense and transitions work?
PrevNext