How does React.memo() optimize functional components in React? (original) (raw)

Last Updated : 24 Apr, 2025

**React.memo() is a higher-order component provided by React that can help optimize functional components by reducing unnecessary re-renders.

Let's explore how it works and how it optimizes functional components:

Table of Content

Optimizing with React.memo():

When to use React.memo() ?

When incorporating React.memo(), it's crucial to exercise caution, as excessively memoizing components can lead to reduced performance by introducing unnecessary overhead. To optimize effectively, consider the following best practices:

**Benefits of memoization using React.memo():

Strike a balance in optimizing and memoization efforts, avoiding unnecessary complexities while enhancing performance.

How to use React.memo() ?

React.memo() is easy to use and intuitive. Here's how to use React.memo() to memoize a functional component step-by-step:

**Step 1: Import your React.memo() file at the top.

import React, { memo } from 'react';

**Step 2: To define your functional component using the below syntax.

const FunctionalComponet = ({ prop1, prop2 }) => {
return

{prop1} {prop2}
;
};

**Step 3: To Memoize your component by wrapping it with React.memo():

const ToMemoized = memo(FuntionalComponent);

**Step 4: To use your memoized component in your parent component using the following syntax:

const ComponentParent = () => {
return (




);
};

By applying React.memo() to your functional component, it is now memoized, ensuring that it will only re-render when there are changes in its props. The usage of memo is straightforward and simplifies the management of component rendering.

**Example: In this example, `****FuntionalComponent**` is wrapped with `****React.memo()**`, which memoizes the component based on its props. If the `prop1` and `prop2` props remain the same between renders, React will reuse the previous render result, preventing unnecessary re-renders.

JavaScript `

import React from 'react';

const FunctionalComponent = React.memo(({ prop1, prop2 }) => { // Component logic... return (

Prop 1: {prop1}

Prop 2: {prop2}

); });

export default FunctionalComponent;

JavaScript

import React from 'react'; import FunctionalComponent from './AnotherComponent';

const ComponentParent = () => {

const exampleProp1 = "Hello";
const exampleProp2 = 42;

return (
    <div>
        <h2>Parent Component</h2>
        <FunctionalComponent
            prop1={exampleProp1} prop2={exampleProp2} />
    </div>
);

};

export default ComponentParent;

`

**Output:

Screenshot-2024-03-01-154444

Output

Conclusion:

In summary, `****React.memo()**` optimizes functional components by memoizing them based on their props. By comparing the new props with the previous props and reusing the previous render result when the props haven't changed, `React.memo()` helps reduce unnecessary re-renders and improves the performance of React applications. However, it's essential to use memoization selectively and consider the specific use case before applying it.