codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚛️

React.js

23 / 61 topics
18Context API Basics19Hooks: Introduction and Overview20Using the useState Hook21Using the useEffect Hook22Creating Custom Hooks23Memoization in React
Tutorials/React.js/Memoization in React
⚛️React.js

Memoization in React

Updated 2026-05-15
10 min read

Memoization in React

Introduction

In the world of web development, performance is a critical aspect that can significantly impact user experience. As applications grow in complexity and scale, managing component rendering efficiently becomes essential. One powerful technique to optimize rendering is memoization.

Memoization is an optimization strategy used to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again. In React, memoization can be particularly useful for optimizing components that are computationally expensive or render frequently with the same props.

Concept

React provides a built-in way to memoize functional components using the React.memo higher-order component (HOC). When you wrap a component with React.memo, React will perform a shallow comparison of its props. If the props have not changed, React will skip re-rendering the component and reuse the last rendered result.

How It Works

  1. Shallow Comparison: React.memo performs a shallow comparison between the current and previous props. If any prop has changed, the component will re-render.
  2. Custom Comparison Function: You can also provide a custom comparison function to control when the component should re-render.

Examples

Let's dive into some practical examples to understand how memoization works in React.

Basic Example

Consider a simple Counter component that increments a count and displays it:

jsx
1import React, { useState } from 'react';
2
3const Counter = ({ increment }) => {
4const [count, setCount] = useState(0);
5
6return (
7 <div>
8 <p>Count: {count}</p>
9 <button onClick={() => setCount(count + increment)}>Increment</button>
10 </div>
11);
12};
13
14export default Counter;

Now, let's wrap this component with React.memo:

jsx
1import React, { useState } from 'react';
2import Counter from './Counter';
3
4const App = () => {
5const [incrementValue, setIncrementValue] = useState(1);
6
7return (
8 <div>
9 <Counter increment={incrementValue} />
10 <button onClick={() => setIncrementValue(incrementValue + 1)}>Change Increment</button>
11 </div>
12);
13};
14
15export default App;

In this example, every time the App component re-renders (e.g., when the "Change Increment" button is clicked), the Counter component will also re-render. However, if you wrap Counter with React.memo, it will only re-render when its props (increment) change.

jsx
1import React, { useState } from 'react';
2import Counter from './Counter';
3
4const App = () => {
5const [incrementValue, setIncrementValue] = useState(1);
6
7return (
8 <div>
9 <React.memo(Counter) increment={incrementValue} />
10 <button onClick={() => setIncrementValue(incrementValue + 1)}>Change Increment</button>
11 </div>
12);
13};
14
15export default App;

Custom Comparison Function

Sometimes, a shallow comparison might not be sufficient. You can provide a custom comparison function to React.memo to control when the component should re-render.

jsx
1import React, { useState } from 'react';
2
3const areEqual = (prevProps, nextProps) => {
4return prevProps.increment === nextProps.increment;
5};
6
7const Counter = ({ increment }) => {
8const [count, setCount] = useState(0);
9
10return (
11 <div>
12 <p>Count: {count}</p>
13 <button onClick={() => setCount(count + increment)}>Increment</button>
14 </div>
15);
16};
17
18export default React.memo(Counter, areEqual);

In this example, the areEqual function checks if the increment prop has changed. If it hasn't, the component will not re-render.

What's Next?

Understanding memoization is just one step in optimizing your React applications. In the next section, we'll explore Lazy Loading Components, a technique to improve performance by deferring the loading of components until they are needed.

By leveraging these techniques, you can create more efficient and responsive React applications that provide a better user experience.


PreviousCreating Custom HooksNext Introduction to React Router

Recommended Gear

Creating Custom HooksIntroduction to React Router