Skip to content
- Tutorials
- Courses
- Go Premium
-
ReactJS Reconciliation
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.
Reconciliation helps in the following ways:
- Minimizes unnecessary updates: React only changes the parts of the UI that actually need to be updated, rather than re-rendering the entire page.
- Improves performance: By optimizing the update process, React reduces the number of changes to the actual DOM, which improves the performance of your application.
- Ensures consistency: React ensures that the UI always matches the current state of your application, even as it changes over time.
How ReactJS Reconciliation Works
The reconciliation process involves the following steps:
1. Render Phase
- React calls the render() method of a component to generate a new virtual DOM representation.
- This new Virtual DOM is compared with the previous Virtual DOM snapshot.
2. Diffing Algorithm
- React compares the old and new virtual DOM trees to determine the differences.
- Instead of re-rendering the entire UI, React updates only the changed nodes.
3. Commit Phase
- Once the differences are determined, React applies the updates to the real DOM in the most efficient way.
- React batches updates and minimizes reflows and repaints for better performance.
Virtual DOM in React
virtual Dom
The Virtual DOM (VDOM) is a lightweight, in-memory representation of the actual DOM elements.
- It allows React to perform DOM updates more efficiently by updating only the necessary elements, avoiding full re-renders of the entire UI.
- The Virtual DOM is a concept borrowed from the idea of "virtualization" in computing, where operations are performed on a virtual version of an object (like the DOM) before applying those changes to the actual object. This results in a more optimized and less resource-intensive approach.
How Virtual DOM Works
- Initial Rendering: React creates an initial Virtual DOM tree when the components are first rendered. React then compares the Virtual DOM with the actual DOM and updates the actual DOM only where necessary.
- State/Props Changes: When the state or props of a component change, React creates a new Virtual DOM tree. React then compares the new Virtual DOM with the previous one, determining what parts of the actual DOM need to be updated.
- Efficient DOM Updates: React uses the diffing algorithm to identify the differences between the new and previous Virtual DOM trees, updating only the parts of the DOM that have changed.
Virtual DOM in ReactDiffing Algorithm and Its Role in Reconciliation
The Diffing Algorithm plays a crucial role in the Reconciliation process. It is responsible for
- Identifying Differences: The diffing algorithm identifies the differences between the old and new Virtual DOM trees by comparing each element.
- Minimizing DOM Changes: The algorithm ensures that only the minimal number of changes are applied to the actual DOM.
- Optimization: The diffing algorithm optimizes updates by reusing elements where possible and only making necessary changes.
Diffing AlgorithmStrategies 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 (
<div>
<h2>Count: {this.state.count}</h2>
<button onClick={this.increment}>Increment</button>
</div>
);
}
}
export default Counter;
Output:
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 <h2>Count: {count}</h2>;
});
function App() {
const [count, setCount] = useState(0);
const [text, setText] = useState('');
return (
<div>
<DisplayCounter count={count} />
<button onClick={() => setCount(count + 1)}>Increment</button>
<input
type="text"
placeholder="Type something"
value={text}
onChange={(e) => setText(e.target.value)}
/>
</div>
);
}
export default App;
Output:
output3. 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 (
<ul>
{users.map((user) => (
<li key={user.id}>
{user.name}
</li>
))}
</ul>
);
}
export default UserList;
Output
output4. 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 <h2>Count: {this.props.count}</h2>;
}
}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 (
<div>
<Display count={this.state.count} />
<button onClick={this.increment}>Increment</button>
<button onClick={this.updateDummy}>Change Dummy</button>
</div>
);
}
}
export default App;
Output
output
Similar Reads
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
React Fundamentals
React IntroductionReactJS is a component-based JavaScript library used to build dynamic and interactive user interfaces. It simplifies the creation of single-page applications (SPAs) with a focus on performance and maintainability. "Hello, World!" Program in ReactJavaScriptimport React from 'react'; function App() {
6 min read
React Environment SetupTo run any React application, we need to first setup a ReactJS Development Environment. In this article, we will show you a step-by-step guide to installing and configuring a working React development environment.Pre-requisite:We must have Nodejs installed on our PC. So, the very first step will be
3 min read
React JS ReactDOMReactDOM is a core React package that provides DOM-specific methods to interact with and manipulate the Document Object Model (DOM), enabling efficient rendering and management of web page elements. ReactDOM is used for: Rendering Components: Displays React components in the DOM.DOM Manipulation: Al
2 min read
React JSXJSX stands for JavaScript XML, and it is a special syntax used in React to simplify building user interfaces. JSX allows you to write HTML-like code directly inside JavaScript, enabling you to create UI components more efficiently. Although JSX looks like regular HTML, itâs actually a syntax extensi
5 min read
ReactJS Rendering ElementsIn this article we will learn about rendering elements in ReactJS, updating the rendered elements and will also discuss about how efficiently the elements are rendered.What are React Elements?React elements are the smallest building blocks of a React application. They are different from DOM elements
3 min read
React ListsIn lists, React makes it easier to render multiple elements dynamically from arrays or objects, ensuring efficient and reusable code. Since nearly 85% of React projects involve displaying data collectionsâlike user profiles, product catalogs, or tasksâunderstanding how to work with lists.To render a
4 min read
React FormsIn React, forms are used to take input from users, like text, numbers, or selections. They work just like HTML forms but are often controlled by React state so you can easily track and update the input values.Example:JavaScriptimport React, { useState } from 'react'; function MyForm() { const [name,
4 min read
ReactJS KeysA key serves as a unique identifier in React, helping to track which items in a list have changed, been updated, or removed. It is particularly useful when dynamically creating components or when users modify the list. When rendering a list, you need to assign a unique key prop to each element in th
4 min read
Components in React
React Lifecycle In React, the lifecycle refers to the various stages a component goes through. These stages allow developers to run specific code at key moments, such as when the component is created, updated, or removed. By understanding the React lifecycle, you can better manage resources, side effects, and perfo
7 min read
React Hooks
Routing in React
Advanced React Concepts
React Projects
`;
$(commentSectionTemplate).insertBefore(".article--recommended");
}
loadComments();
});
});
function loadComments() {
if ($("iframe[id*='discuss-iframe']").length top_of_element && top_of_screen articleRecommendedTop && top_of_screen articleRecommendedBottom)) {
if (!isfollowingApiCall) {
isfollowingApiCall = true;
setTimeout(function(){
if (loginData && loginData.isLoggedIn) {
if (loginData.userName !== $('#followAuthor').val()) {
is_following();
} else {
$('.profileCard-profile-picture').css('background-color', '#E7E7E7');
}
} else {
$('.follow-btn').removeClass('hideIt');
}
}, 3000);
}
}
});
}
$(".accordion-header").click(function() {
var arrowIcon = $(this).find('.bottom-arrow-icon');
arrowIcon.toggleClass('rotate180');
});
});
window.isReportArticle = false;
function report_article(){
if (!loginData || !loginData.isLoggedIn) {
const loginModalButton = $('.login-modal-btn')
if (loginModalButton.length) {
loginModalButton.click();
}
return;
}
if(!window.isReportArticle){
//to add loader
$('.report-loader').addClass('spinner');
jQuery('#report_modal_content').load(gfgSiteUrl+'wp-content/themes/iconic-one/report-modal.php', {
PRACTICE_API_URL: practiceAPIURL,
PRACTICE_URL:practiceURL
},function(responseTxt, statusTxt, xhr){
if(statusTxt == "error"){
alert("Error: " + xhr.status + ": " + xhr.statusText);
}
});
}else{
window.scrollTo({ top: 0, behavior: 'smooth' });
$("#report_modal_content").show();
}
}
function closeShareModal() {
const shareOption = document.querySelector('[data-gfg-action="share-article"]');
shareOption.classList.remove("hover_share_menu");
let shareModal = document.querySelector(".hover__share-modal-container");
shareModal && shareModal.remove();
}
function openShareModal() {
closeShareModal(); // Remove existing modal if any
let shareModal = document.querySelector(".three_dot_dropdown_share");
shareModal.appendChild(Object.assign(document.createElement("div"), { className: "hover__share-modal-container" }));
document.querySelector(".hover__share-modal-container").append(
Object.assign(document.createElement('div'), { className: "share__modal" }),
);
document.querySelector(".share__modal").append(Object.assign(document.createElement('h1'), { className: "share__modal-heading" }, { textContent: "Share to" }));
const socialOptions = ["LinkedIn", "WhatsApp","Twitter", "Copy Link"];
socialOptions.forEach((socialOption) => {
const socialContainer = Object.assign(document.createElement('div'), { className: "social__container" });
const icon = Object.assign(document.createElement("div"), { className: `share__icon share__${socialOption.split(" ").join("")}-icon` });
const socialText = Object.assign(document.createElement("span"), { className: "share__option-text" }, { textContent: `${socialOption}` });
const shareLink = (socialOption === "Copy Link") ?
Object.assign(document.createElement('div'), { role: "button", className: "link-container CopyLink" }) :
Object.assign(document.createElement('a'), { className: "link-container" });
if (socialOption === "LinkedIn") {
shareLink.setAttribute('href', `https://www.linkedin.com/sharing/share-offsite/?url=${window.location.href}`);
shareLink.setAttribute('target', '_blank');
}
if (socialOption === "WhatsApp") {
shareLink.setAttribute('href', `https://api.whatsapp.com/send?text=${window.location.href}`);
shareLink.setAttribute('target', "_blank");
}
if (socialOption === "Twitter") {
shareLink.setAttribute('href', `https://twitter.com/intent/tweet?url=${window.location.href}`);
shareLink.setAttribute('target', "_blank");
}
shareLink.append(icon, socialText);
socialContainer.append(shareLink);
document.querySelector(".share__modal").appendChild(socialContainer);
//adding copy url functionality
if(socialOption === "Copy Link") {
shareLink.addEventListener("click", function() {
var tempInput = document.createElement("input");
tempInput.value = window.location.href;
document.body.appendChild(tempInput);
tempInput.select();
tempInput.setSelectionRange(0, 99999); // For mobile devices
document.execCommand('copy');
document.body.removeChild(tempInput);
this.querySelector(".share__option-text").textContent = "Copied"
})
}
});
// document.querySelector(".hover__share-modal-container").addEventListener("mouseover", () => document.querySelector('[data-gfg-action="share-article"]').classList.add("hover_share_menu"));
}
function toggleLikeElementVisibility(selector, show) {
document.querySelector(`.${selector}`).style.display = show ? "block" : "none";
}
function closeKebabMenu(){
document.getElementById("myDropdown").classList.toggle("show");
}