Re-rendering Components in ReactJS
Last Updated :
07 Apr, 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>
);
}
Similar Reads
ReactJS Rendering Elements
In 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 different from DOM elements as React elements are simple JavaScript objects and are effi
3 min read
ReactJS Server Components
React is a JavaScript library which is used to develop frontend of websites. Since it's introduction in the year 2013 React has gained a lot of popularity among the web developers around the world. The library has a great support from the community and is evolving at a high speed from its launch. In
3 min read
ReactJS Pure Components
ReactJS 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
Conditional rendering component using Enums in ReactJS
In certain scenarios, a ReactJS developer may have to dynamically display or hide components depending on specific conditions. For instance, when creating a To-Do list application, the developer must render tasks if there are pending items; otherwise, they should display a message such as "No pendin
3 min read
ReactJS Onsen UI Page Component
ReactJS Onsen-UI is a popular front-end library with a set of React components that are designed to developing HTML5 hybrid and mobile web apps in a beautiful and efficient way. Page components are used for handling the entire page. The content can be scrolled. We can use the following approach in R
3 min read
How to conditionally render components in ReactJS ?
React is a declarative, efficient, and flexible JavaScript library for building user interfaces. Itâs âVâ in MVC. ReactJS is an open-source, component-based front-end library responsible only for the view layer of the application. It is maintained by Facebook. PrerequisitesNodeJS or NPMReact JSReact
3 min read
ReactJS | Components - Set 2
In our previous article on ReactJS | Components we had to discuss components, types of components, and how to render components. In this article, we will see some more properties of components. Composing Components: Remember in our previous article, our first example of GeeksforGeeks's homepage whic
3 min read
ReactJS Reactstrap List Component
Reactstrap is a popular front-end library that is easy to use React Bootstrap 4 components. This library contains the stateless React components for Bootstrap 4. The List component allows the user to display a list. We can use the following approach in ReactJS to use the ReactJS Reactstrap List Comp
3 min read
React Rebass Link Component
React Rebass is a front-end framework that was designed keeping react in mind. In this article, we will know how to use Link Component in React Rebass. The Link is an important component that is required in each development. So to create a Link component we can import the Rebass Link component. The
2 min read
ReactJS Components Complete Reference
In ReactJS, components are the building blocks that help you create interactive UIs by dividing your app into smaller, reusable pieces of code. Understanding how components work is essential for efficiently developing React applications. In this article will provide a complete reference to React com
3 min read