ReactJS Reconciliation (original) (raw)

Last Updated : 13 Aug, 2025

**Reconciliation is the process React uses to figure out how to efficiently update the DOM (Document Object Model) when changes occur in the UI. React's goal is to update the page as efficiently as possible, without unnecessary re-rendering or slow performance.

frame_4

**Reconciliation helps in the following ways:

How ReactJS Reconciliation Works

**The reconciliation process involves the following steps:

**1. Render Phase

**2. Diffing Algorithm

**3. Commit Phase

Virtual DOM in React

virtualdom

virtual Dom

The Virtual DOM (VDOM) is a lightweight, in-memory representation of the actual DOM elements.

How Virtual DOM Works

virtual-Dom

Virtual DOM in React

Diffing Algorithm and Its Role in Reconciliation

The Diffing Algorithm plays a crucial role in the Reconciliation process. It is responsible for

Diffing-Algo

Diffing Algorithm

Strategies for Optimizing ReactJS Reconciliation

1. Use shouldComponentUpdate

In class components, React provides the shouldComponentUpdate() lifecycle method, which allows us to prevent re-renders if the component’s state or props haven’t changed.

index.js `

import React from 'react';

class Counter extends React.Component { constructor(props) { super(props); this.state = { count: 0, }; }

// Only re-render if count is an even number shouldComponentUpdate(nextProps, nextState) { console.log('shouldComponentUpdate called'); if (nextState.count % 2 === 0) { return true; // Allow re-render } return false; // Skip re-render }

increment = () => { this.setState((prevState) => ({ count: prevState.count + 1 })); };

render() { console.log('Render called'); return (

Count: {this.state.count}

Increment
); } }

export default Counter;

`

Output:

reactoutput1

shouldComponentUpdate()

2. Use React.memo

React.memo is a higher-order component for functional components that prevents re-renders if the props haven't changed. It’s similar to shouldComponentUpdate but easier to use with functional components.

index.js `

import React, { useState } from 'react';

// Child component wrapped in React.memo const DisplayCounter = React.memo(({ count }) => { console.log('DisplayCounter rendered'); return

Count: {count}

; });

function App() { const [count, setCount] = useState(0); const [text, setText] = useState('');

return (

<button onClick={() => setCount(count + 1)}>Increment <input type="text" placeholder="Type something" value={text} onChange={(e) => setText(e.target.value)} />
); }

export default App;

`

Output:

output221

output

3. Use key Prop Efficiently in Lists

When rendering lists of elements, always use a unique key for each item. This helps React efficiently track and update individual elements without reordering or re-rendering the entire list.

JavaScript `

import React from 'react'; const users = [ { id: 101, name: 'Alice' }, { id: 102, name: 'Bob' }, { id: 103, name: 'Charlie' }, ];

function UserList() { return (

); }

export default UserList;

`

Output

output44

output

4. Avoid Inline Functions and Objects in JSX

Inline functions and objects in JSX create a new instance on every render, which can cause unnecessary re-renders. Instead, define functions and objects outside of the render cycle or use useCallback to memoize them.

5. Use React.PureComponent

React.PureComponent is a base class for class components that implements shallow comparison of props and state to prevent unnecessary re-renders.

JavaScript `

import React from 'react'; // PureComponent to avoid re-rendering if props haven't changed class Display extends React.PureComponent { render() { console.log('Display re-rendered'); return

Count: {this.props.count}

; } }c

class App extends React.Component { state = { count: 0, dummy: '', };

increment = () => { this.setState({ count: this.state.count + 1 }); };

updateDummy = () => { this.setState({ dummy: 'Changed' }); // Won't affect Display if count doesn't change };

render() { return (

Increment Change Dummy
); } }

export default App;

`

Output

output55

output

What is Reconciliation in React?

Explanation:

Reconciliation refers to **how React determines what needs to change in the DOM, updating only modified parts for better performance.

Which algorithm is responsible for comparing old and new Virtual DOM trees during Reconciliation?

Explanation:

React uses the **Diffing Algorithm to compare previous and new Virtual DOM trees and apply minimal updates to the real DOM.

Which method helps prevent unnecessary re-renders in class components?

Explanation:

The article states that **shouldComponentUpdate() is used in class components to stop a re-render if props/state haven't changed.

What is the main benefit of using keys in a list during Reconciliation?

Explanation:

Unique keys help React **track list items individually, improving performance by avoiding unnecessary re-renders or reordering.

Quiz Completed Successfully

Your Score : 2/4

Accuracy : 0%

Login to View Explanation

1/4

1/4 < Previous Next >