๐Ÿ”ฅ 30 Days of Frontend โ€” Day 2: React Reconciliation

๐Ÿ”ฅ 30 Days of Frontend โ€” Day 2: React Reconciliation

Leader โ—1 โ—7 โ—16
calendar_today โ€ข schedule2 min read

Why doesn't React update the entire UI after every state change?

Because React uses reconciliation.

When a component renders again, React produces a new element tree.

React then determines what changed compared with the previous result and prepares the necessary updates.

Think of it as:

Previous tree + New tree
          โ†“
    Reconciliation
          โ†“
   Required updates

๐Ÿง  A Simple Example

Imagine we have a list of todos:

function TodoList({ todos }) {
  return (
    <ul>
      {todos.map((todo) => (
        <TodoItem key={todo.id} todo={todo} />
      ))}
    </ul>
  );
}

Initially, our list looks like:

[A, B, C]

Then it changes to:

[A, B, D]

Because we're using stable keys, React can reason about the identity of each item:

A โ†’ same item
B โ†’ same item
C โ†’ removed
D โ†’ new item

The important part isn't just that the data changed.

It's that React can understand which elements represent the same identity across renders.

๐Ÿ”‘ Why Do Keys Matter?

Consider this:

items.map((item) => (
  <Item key={Math.random()} />
))

Every render produces a different key.

That makes it difficult for React to identify the item as the same item from the previous render.

Instead, use a stable identifier:

items.map((item) => (
  <Item key={item.id} />
))

Now React has a stable way to understand:

"This is the same item as before."

This becomes especially important when components contain local state.

For example, if each Item contains an input field, unstable keys can cause component instances to be recreated and lead to unexpected state behavior.

๐Ÿงฉ A Useful Mental Model

When debugging list rendering, don't only ask:

"Why did this component render?"

Also ask:

"Does React know this is the same component as before?"

That question can help explain many confusing behaviors involving:

  • List keys
  • Component identity
  • Local state
  • Re-renders
  • Elements being added or removed

โš™๏ธ Where Does Reconciliation Happen?

Reconciliation happens during React's render phase.

React determines what work needs to be done first. The resulting changes are then applied during the commit phase.

A simplified mental model is:

State Update
     โ†“
Render
     โ†“
Reconciliation
     โ†“
Determine required changes
     โ†“
Commit
     โ†“
DOM updates

It's important to remember that rendering does not mean React immediately changes the DOM.

The render phase is where React figures out what the next UI should look like.

The commit phase is where React applies the necessary changes to the DOM.

๐ŸŽฏ The Takeaway

Reconciliation is one of the core ideas behind how React updates the UI efficiently.

Once you understand reconciliation, concepts like keys, component identity, and rendering behavior become much easier to reason about.

Part 2 of 5 in 30 Days of Frontend

1 Comment

2 votes
๐Ÿ”ฅ Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

How I Built a React Portfolio in 7 Days That Landed โ‚น1.2L in Freelance Work

Dharanidharan - Feb 9

How to Build a Portfolio Website That Actually Gets You Hired

muhammadfarhan.dev - Aug 21

My 30-day log cleanup kept a file that was 30 and a half days old

BashSnippets - Oct 8

SolidJS 2.0 Async Data: A Deep Dive for React Devs

morellodev - Jul 16

Understanding React Hooks: useState and useEffect Explained Simply

muhammadfarhan.dev - Jul 28
chevron_left
1.2k Points โ€ข 24 Badges
Alexandria, Egypt โ€ข behance.net/alaasamy12
5Posts
2Comments
4Connections

Related Jobs

View all jobs โ†’

Commenters (This Week)

4 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!