Memoization in React

Lately, I have been working on some old React applications. During this time I learnt and implemented some techniques to optimize and improve the performance of a React application. One such technique is Memoization. Here in this blog, I will talk about how to apply Memoization in a React application. We will see how the React.memo() function can be used to avoid unnecessary re-renders. We will also check the useCallback and useMemo hooks and how to use them alongside the React.memo() higher-order function. In the end, we will discuss practical use cases on when to apply memoization and when to skip. In technical terms - Memoization in software engineering is a technique of caching the results of expensive operations and reusing them whenever the same operation is called. Using this technique, redundant time-consuming operations are skipped, thus improving performance. Before we move on to the actual topic, let’s revise some basics first. React is a JavaScript library used for building user interfaces. It follows a component-based architecture, where the UI is divided into small, reusable components that function independently based on their state. A state can be thought of as the local memory of a component, responsible for holding its dynamic data. React ensures the UI stays up-to-date by re-rendering components whenever the state of that component changes. In essence, any change in a component’s state triggers a re-render to reflect the updated change on the UI. A Component in React will re-render on two occasions If the state of that component changes. If the Parent component of that component re-renders. For example, if we have Component A, which is a parent of Component B, then every time Component A re-renders, B will automatically re-render itself since it is the child of that Component. See the image below for a better understanding. By triggering a component re-render, React maintains the sync between all the components and keeps the UI updated with the latest changes. But this very own re-rendering property of React also is responsible for slowing down the application. You see, re-rendering a component means re-creating the entire Component along with the variables and functions inside it from scratch. This process is not always simple and inexpensive. For example, look at the below image When Component A re-renders, all components within its branch(child components) will also be re-rendered. This means that even if a component didn't undergo state change, it will still be re-rendered again since its parent got re-rendered. This at times is unnecessary and inefficient. So as per the above image, if Component A re-renders, Component B, C, and D will be re-rendered. If these components involve resource-intensive operations such as data fetching, initializing large objects or arrays, or running computationally expensive functions, those operations will be executed again. This situation becomes even more problematic if Component X triggers a re-render. In such a case, not only will the entire Branch A(children of A) re-render, but so will Branch B(children of B), causing significant performance issues. Hence, the inherent behavior of React, which involves re-rendering, can also contribute to performance degradation. Now that we have identified the problem, let's explore how we can address it effectively. Enough talk, let’s start writing some code. In the below code, I have written a simple Parent component with a count state, a variable displaying the count value, and a to update the count. Inside the Parent component, I have added a simple Child component that takes no props. import { useState } from 'react' function Child() { console.log("Child component rendered"); return ( This is child component ) } export default function Parent() { const [count, setCount] = useState(0); console.log("Parent component rendered"); return ( setCount((count) => count + 1)}> count is {count} ) } After running the above code in the browser we can see the below output in the browser's console. Try clicking the to change the count. If you see the console again, you will observe that not only the Parent component is re-rendered but also the Child component. This is the unnecessary re-rendering of the Child component problem that we discussed earlier. Component rendering can be a heavy task that might affect the overall performance of an application. To avoid this unnecessary re-rendering of components, we can use the React.memo() higher-order function. What is React.memo(): React.memo() is a higher-order component provided by React to create a memoized version of a component. A memoized component makes a shallow comparison of the previous and new props. If the props are different, the components re-render while if the props are the same the component re-rendering

Feb 16, 2025 - 16:05
 0
Memoization in React

Lately, I have been working on some old React applications. During this time I learnt and implemented some techniques to optimize and improve the performance of a React application. One such technique is Memoization.

Here in this blog, I will talk about how to apply Memoization in a React application. We will see how the React.memo() function can be used to avoid unnecessary re-renders. We will also check the useCallback and useMemo hooks and how to use them alongside the React.memo() higher-order function. In the end, we will discuss practical use cases on when to apply memoization and when to skip.

In technical terms -

Memoization in software engineering is a technique of caching the results of expensive operations and reusing them whenever the same operation is called. Using this technique, redundant time-consuming operations are skipped, thus improving performance.

Before we move on to the actual topic, let’s revise some basics first.

React is a JavaScript library used for building user interfaces. It follows a component-based architecture, where the UI is divided into small, reusable components that function independently based on their state.

A state can be thought of as the local memory of a component, responsible for holding its dynamic data. React ensures the UI stays up-to-date by re-rendering components whenever the state of that component changes. In essence, any change in a component’s state triggers a re-render to reflect the updated change on the UI.

A Component in React will re-render on two occasions

  1. If the state of that component changes.
  2. If the Parent component of that component re-renders.

For example, if we have Component A, which is a parent of Component B, then every time Component A re-renders, B will automatically re-render itself since it is the child of that Component. See the image below for a better understanding.

parent-child component relationship

By triggering a component re-render, React maintains the sync between all the components and keeps the UI updated with the latest changes. But this very own re-rendering property of React also is responsible for slowing down the application. You see, re-rendering a component means re-creating the entire Component along with the variables and functions inside it from scratch. This process is not always simple and inexpensive. For example, look at the below image

React Component Structure

When Component A re-renders, all components within its branch(child components) will also be re-rendered. This means that even if a component didn't undergo state change, it will still be re-rendered again since its parent got re-rendered. This at times is unnecessary and inefficient. So as per the above image, if Component A re-renders, Component B, C, and D will be re-rendered.

Parent Component causing component re-rendering - React

If these components involve resource-intensive operations such as data fetching, initializing large objects or arrays, or running computationally expensive functions, those operations will be executed again. This situation becomes even more problematic if Component X triggers a re-render. In such a case, not only will the entire Branch A(children of A) re-render, but so will Branch B(children of B), causing significant performance issues.

Grandparent Component causing component re-rendering - React

Hence, the inherent behavior of React, which involves re-rendering, can also contribute to performance degradation. Now that we have identified the problem, let's explore how we can address it effectively.

Enough talk, let’s start writing some code. In the below code, I have written a simple Parent component with a count state, a variable displaying the count value, and a