Re-rendering Components in ReactJS
Last Updated :
23 Jul, 2025
Re-rendering is an important concept in ReactJS as it determines how and when components update. Understanding the re-rendering process is essential for optimizing the performance of React applications.
What is Re-rendering in ReactJS?
In React, a re-render happens when a component's state or props change. React then compares the new version of the component with the previous one, and if something has changed, it re-renders the component to reflect the updated state. The primary goal of re-rendering is to ensure the UI stays in sync with the underlying state and props.
Re-rendering occurs under the following scenarios:
- State and Props: Re-rendering components in ReactJS happens when their state or the props change.
- Virtual DOM: React uses the concept of the virtual DOM to compare the current state of the UI with the new state and then updates only the necessary part.
- Component Lifecycle: Re-renders in class components are controlled by lifecycle methods, while hooks manage them in functional components.
- Force Update: You can force a re-render using forceUpdate() in class components, though this is rarely needed.
How React Handles Re-Rendering?
React uses the Virtual DOM concept for handling the re-rendering:
- Initial Render: React creates a virtual DOM tree representing the UI based on the current state and props of components.
- State or Props Change: When the state or props change, React updates the component’s virtual DOM and compares it with the previous virtual DOM using a process called reconciliation.
- Diffing Algorithm: React’s diffing algorithm identifies the differences between the current and previous virtual DOM. Only the changes are applied to the real DOM, which minimizes performance overhead.
- Re-rendering: React re-renders the components with the new state or props, and only the necessary DOM updates are applied.
Example: React Re-rendering with State
JavaScript
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
export default Counter;
Output
State in React
In this example
- Initially the component rerenders with the count = 0.
- When we click the button the state updates, which trigger the re-rendering.
- Now, the React will compare with the virtual Dom and will update the only changed part.
When Does React Re-render a Component?
Re-rendering components in React happens when
- State Change: Using setState in class components or useState in functional components triggers a re-render.
const [count, setCount] = useState(0);const increment = () => setCount(count + 1);
- Props Changes: When a parent component passes new props to a child, the child component re-renders.
<ChildComponent value={newValue} />
- Force Update: Although rarely needed, you can force a re-render using forceUpdate() in class components.
this.forceUpdate();
Context Changes: Components consuming context via useContext will re-render when the context value changes.
const value = useContext(MyContext);
Identifying Unnecessary Re-renders
Due the unnecessary re-renders the can slow the performance. So, we can identify them by using below methods:
- React Developer Tools Profiler: It shows the re-rendering of the component.
- Console Logs: It helps to track the renders inside the render function.
How React Optimizes Re-rendering?
React optimizes re-rendering using several techniques to ensure performance:
- Virtual DOM: React maintains a lightweight virtual DOM that helps minimize direct DOM manipulations.
- Diffing Algorithm: React compares the new virtual DOM with the previous version to detect changes efficiently.
- Batched Updates: React batches multiple state updates together to reduce the number of re-renders.
- Memoization: React provides tools like React.memo and useMemo to prevent unnecessary re-renders.
How to Control Re-rendering in React
Controlling unnecessary re-renders is essential for optimizing performance, especially in large applications. Here are some techniques to manage re-rendering
1. React.memo
React.memo
is a higher-order component (HOC) that can be used to wrap functional components to prevent unnecessary re-renders. It only re-renders if the props change, similar to how PureComponent
works for class components.
XML
const MyComponent = React.memo((props) => {
return <div>{props.name}</div>;
});
2. shouldComponentUpdate
The shouldComponentUpdate lifecycle method allows you to control whether a component should re-render or not. By returning false in shouldComponentUpdate, you can prevent re-renders even when state or props change.
XML
class MyComponent extends React.Component {
shouldComponentUpdate(nextProps, nextState) {
// Perform custom comparison and return true/false
return nextState.someValue !== this.state.someValue;
}
render() {
return <div>{this.state.someValue}</div>;
}
}
3. useMemo
useMemo is a React Hook that memorizes the result of a computation and returns the cached result unless its dependencies have changed. This is useful to prevent expensive calculations from running on every render.
XML
const compute = useMemo(() => {
return val(props);
}, [props.someDependency]);
4. useCallback
useCallback is similar to useMemo, but it returns a memoized version of the callback function. This ensures that the same function reference is passed on re-renders unless its dependencies change.
XML
const memoizedCallback = useCallback(() => {
}, [props.someDependency]);
5. Lazy Loading and Suspense
React's lazy loading and Suspense allow you to load components only when they are required. This can help reduce the number of re-renders during initial loading by splitting the bundle into smaller chunks.
XML
const LazyComp = React.lazy(() => import('./LazyComponent'));
function MyComp() {
return (
<Suspense fallback={<div>Loading...</div>}>
<LazyComp />
</Suspense>
);
}
Re-rendering Components in ReactJS
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. Why Use React?Before React, web development faced issues like slow DOM updates and mes
7 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 methods to interact with the Document Object Model, or DOM. This package allows developers to access and modify the DOM. It is a package in React that provides DOM-specific methods that can be used at the top level of a web app to enable an efficient wa
3 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 ListsReact Lists are used to display a collection of similar data items like an array of objects and menu items. It allows us to dynamically render the array elements and display repetitive data.Rendering List in ReactTo render a list in React, we will use the JavaScript array map() function. We will ite
5 min read
React FormsForms are an essential part of any application used for collecting user data, processing payments, or handling authentication. React Forms are the components used to collect and manage the user inputs. These components include the input elements like text field, check box, date input, dropdowns etc.
5 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. In this article, we'll explore ReactJS keys, understand their importance, how the
5 min read
Components in React
React ComponentsIn React, React components are independent, reusable building blocks in a React application that define what gets displayed on the UI. They accept inputs called props and return React elements describing the UI.In this article, we will explore the basics of React components, props, state, and render
4 min read
ReactJS Functional ComponentsIn ReactJS, functional components are a core part of building user interfaces. They are simple, lightweight, and powerful tools for rendering UI and handling logic. Functional components can accept props as input and return JSX that describes what the component should render.Stateless (before hooks)
5 min read
React Class ComponentsClass components are ES6 classes that extend React.Component. They allow state management and lifecycle methods for complex UI logic.Used for stateful components before Hooks.Support lifecycle methods for mounting, updating, and unmounting.The render() method in React class components returns JSX el
4 min read
ReactJS Pure ComponentsReactJS Pure Components are similar to regular class components but with a key optimization. They skip re-renders when the props and state remain the same. While class components are still supported in React, it's generally recommended to use functional components with hooks in new code for better p
4 min read
ReactJS Container and Presentational Pattern in ComponentsIn this article we will categorise the react components in two types depending on the pattern in which they are written in application and will learn briefly about these two categories. We will also discuss about alternatives to this pattern. Presentational and Container ComponentsThe type of compon
2 min read
ReactJS PropTypesIn ReactJS PropTypes are the property that is mainly shared between the parent components to the child components. It is used to solve the type validation problem. Since in the latest version of the React 19, PropeTypes has been removed. What is ReactJS PropTypes?PropTypes is a tool in React that he
5 min read
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