Rerendering Components in ReactJS (original) (raw)

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:

How React Handles Re-Rendering?

React uses the Virtual DOM concept for handling the re-rendering:

  1. **Initial Render: React creates a virtual DOM tree representing the UI based on the current state and props of components.
  2. **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.
  3. **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.
  4. **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

Output

State in React

**In this example

When Does React Re-render a Component?

Re-rendering components in React happens when

const [count, setCount] = useState(0);const increment = () => setCount(count + 1);

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:

How React Optimizes Re-rendering?

React optimizes re-rendering using several techniques to ensure performance:

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

{props.name}
; });

`

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

{this.state.someValue}
; } }

`

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={

Loading...
}> ); }

`