Skip to main content

Understanding React Re-renders: Virtual DOM, Diffing & Reconciliation

Understanding React Re-renders: Virtual DOM, Diffing & Reconciliation
react#react#performance#virtual-dom#reconciliation+2 more

Understanding React Re-renders: Virtual DOM, Diffing & Reconciliation

We're all afraid of re-renders. But is this fear justified? Learn how React actually handles changes under the hood and why not every render is a disaster.

Mohammad Alhabil

Author

December 1, 2024
5 min read
~1000 words

Understanding React Re-renders: Virtual DOM, Diffing & Reconciliation

We're all afraid of the word "re-render" — as if it's a curse that affects performance and ruins the user experience.

But... is this fear justified? Or is the problem that we don't understand what's happening under the hood?

Today, let me explain simply, with a bit of depth, how React actually handles changes — and why not every render should be considered a "danger" or "disaster."

🚀 The Story from the Beginning

When React does a re-render, it doesn't mean it's rebuilding the entire page. No!

Here's what actually happens:

StepWhat Happens
1️⃣Creates a new copy of the Virtual DOM
2️⃣Compares it with the previous version (Diffing)
3️⃣Only updates changes to the Real DOM (Reconciliation)

Why This Matters

The real DOM is very slow. Every time you modify it, the browser might do:

  • Reflow: Recalculates positions and sizes of elements
  • Repaint: Redraws pixels on the screen

React tries to avoid this scenario as much as possible... and relies on you to help it work smartly.

👨‍💻 How to Help React as a Developer

1. Understand When Re-render Happens

Re-renders occur when:

  • ✅ Props or state changes
  • ✅ Shared context value changes
  • ✅ Parent re-renders (usually pulling children with it)

2. Use Tools to Reduce Unnecessary Renders

React.memo

Prevents re-render if props haven't changed:

const MyComponent = React.memo(({ data }) => {
  return <div>{data.title}</div>;
});

useCallback & useMemo

Stabilize heavy values and functions:

// Memoize a function
const handleClick = useCallback(() => {
  doSomething(id);
}, [id]);

// Memoize a computed value
const expensiveValue = useMemo(() => {
  return computeExpensiveValue(data);
}, [data]);

⚠️ Warning: Don't use them randomly. If overused, they can actually hurt performance!

Smart Keys in Lists

// ❌ Bad - using index as key
{posts.map((post, index) => (
  <Post key={index} data={post} />
))}

// ✅ Good - using unique identifier
{posts.map((post) => (
  <Post key={post.id} data={post} />
))}

Using key={index} can make React redraw the entire list even if only one item changed!

Solution: Give each element a stable, unique key like post.id. This way React knows exactly what changed and only updates that.

🧠 Debugging Tool: React Profiler

The Profiler is a diagnostic tool inside React DevTools that shows:

  • 📊 Which components are re-rendering
  • 🔢 How many times they rendered
  • ⏱️ How long each render took
  • ❓ What caused the render (state? props? context?)

How to Use It

  1. Install React DevTools browser extension
  2. Open DevTools → "Profiler" tab
  3. Click "Record" → Interact with your app → Stop recording
  4. Analyze the flame graph

Pro Tip: As soon as your project grows, open the Profiler, look for bottlenecks, and start optimizing from there.

🎯 Key Takeaways

ConceptWhat to Remember
Re-rendersNot always dangerous, but know why they happen
Smart KeysHelp React do Reconciliation accurately
ProfilerHelps you understand, not solve
Internal PhasesVirtual DOM → Diffing → Reconciliation

The more you understand the internal phases, the cleaner, faster, and smarter code you can write.

The Flow Visualized

State/Props Change
       ↓
  Virtual DOM (New)
       ↓
    Diffing
  (Compare with old)
       ↓
  Reconciliation
(Update only changes)
       ↓
   Real DOM
  (Minimal updates)

Start understanding the UI, not just building it.

Interfaces you love, code you understand. 💡

Topics covered

#react#performance#virtual-dom#reconciliation#optimization#react-profiler

Found this article helpful?

Share it with your network and help others learn too!

Mohammad Alhabil

Written by Mohammad Alhabil

Frontend Developer & Software Engineer passionate about building beautiful and functional web experiences. I write about React, Next.js, and modern web development.