React.memo: Performance Optimization in Functional Components

Learn how React.memo helps optimize performance in functional components by memoizing the rendered output.

Overview

React.memo is a higher order component that optimizes functional components by memoizing their output. This means that if a component's props do not change, React will skip rendering this component and use the saved result instead. Understanding how and when to use React.memo is crucial for improving performance in React applications.

How it works

React.memo works by comparing the current props of a component with the previous props. If the props remain the same, the component will not re-render. This optimization can significantly reduce unnecessary renders, especially in large applications.

Here’s a minimal example demonstrating how to use React.memo:

import React from 'react';

const MemoizedComponent = React.memo(({ title }) => {
    console.log('Rendering:', title);
    return <h1>{title}</h1>;
});

const ParentComponent = () => {
    const [count, setCount] = React.useState(0);
    return (
        <div>
            <MemoizedComponent title="Hello, World!" />
            <button onClick={() => setCount(count + 1)}>Increment: {count}</button>
        </div>
    );
};

In this example, MemoizedComponent will only re-render if its title prop changes. Clicking the button increments the count, but will not cause MemoizedComponent to re-render since its props remain unchanged.

Comparison Table of React.memo Features

Feature React.memo Regular Functional Component
Re-renders on prop change Only if props change Always re-renders on every parent render
Performance Optimizes rendering for stable props Can lead to unnecessary renders
Usage Best for low-frequency prop changes and static data Suitable for commonly changing props
Custom comparison Allows custom comparison logic No built-in optimization

Common Mistakes

  • Not using React.memo when components receive static props frequently.
  • Assuming React.memo has performance benefits in every case; it can introduce overhead if the props change often.
  • Forgetting to optimize within children components that can still cause re-renders.
  • Misunderstanding React.memo as a solution for every performance issue rather than specific scenarios.

FAQ

Q: What does the react.memo function primarily help with in a React component? A: It helps by memoizing the component's output, preventing unnecessary re-renders when props do not change.

Q: What conditions should I consider before using React.memo? A: Use it when the component renders often with the same props or when expensive operations could be avoided by skipping renders.

Q: Can I pass custom comparison logic to React.memo? A: Yes, you can provide a second argument to React.memo, which is a function that determines whether to re-render based on custom logic.

Q: Does React.memo affect class components? A: No, React.memo works only with functional components. For class components, use shouldComponentUpdate or other optimization techniques.

References

Practice

Ready to practice react-memoization?

Answer real questions, get instant feedback, and watch your skill score climb — free. Practice is in English, like real tech interviews.

Try one 👇

ReactHooksMid
0 XP
When does useEffect run by default?

↑ Go ahead — pick an answer. This is Skillpato.