← Back to React, page 1

react-0012 · Medium mid

What does React.memo do, when does it help, and why does it sometimes fail to stop re-renders?

React.memo wraps a component so React skips re-rendering it when its props are shallowly equal to the previous props. It helps when a parent re-renders often and the child is costly. It fails when a parent creates new objects, arrays or functions for props on every render, because those are never equal by reference. Measure first, then fix the unstable props.

Full answer

By default, when a component re-renders, all of its children re-render too, even if their props did not change. Usually this is fine, since rendering is fast. When a child is slow, wrap it in memo: const Chart = memo(function Chart(props) { ... }).

With memo, React compares each prop of the new render to the previous one using Object.is. If every prop is equal, it reuses the last result and skips the child. Strings, numbers and booleans compare by value. Objects, arrays and functions compare by reference.

That is where memo breaks. In <Badge style={{ color: 'red' }} /> the object is created fresh on every parent render, so the reference differs and the child renders again. The same is true of inline arrow functions and children written as JSX, because each JSX element is a new object. Fixes are to move constants outside the component, memoize the value with useMemo, wrap callbacks in useCallback, or pass primitive props.

The second argument of memo is an optional comparison function. It is rarely needed, and a wrong one causes stale UI, so prefer fixing the props.

memo only looks at props. A memoized component still re-renders when its own state changes, or when a context it reads changes.

Use memo where there is measured benefit: large lists, charts, editors, components that re-render with a fast-changing parent. Do not wrap every component. React Compiler can memoize at build time, which reduces how much manual memo you need.

In the example Badge A gets a new style object every render and Badge B gets a stable one.

jsx
import { memo, useState } from 'react';

const stableStyle = { color: 'green' };

const Badge = memo(function Badge({ label, style }) {
  console.log(label + ' rendered');
  return <span style={style}>{label}</span>;
});

export default function App() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <button onClick={() => setCount((c) => c + 1)}>Re-render parent: {count}</button>
      <Badge label="A (new object each render)" style={{ color: 'red' }} />
      <Badge label="B (stable object)" style={stableStyle} />
    </div>
  );
}

Console output after mounting and clicking the button once. Badge A renders again because its style prop is a new object, while Badge B is skipped: A (new object each render) rendered B (stable object) rendered A (new object each render) rendered

Key points

  • memo skips a re-render when all props are shallowly equal to the last render.
  • Objects, arrays, functions and JSX children are new references on each render and defeat memo.
  • Stabilize props with constants, useMemo or useCallback, or pass primitives.
  • memo does not stop re-renders caused by the component's own state or context.
  • Add it after measuring. React Compiler can handle much of this automatically.

Common mistakes

  • Wrapping a component in memo and passing inline objects, arrays or functions as props.
  • Passing children as JSX to a memoized component and expecting it to be skipped.
  • Writing a custom comparison function that ignores a prop that does change.
  • Adding memo to every component without measuring.

Follow-ups

  • How would you find out which component is re-rendering unnecessarily?
  • What is the difference between memo, useMemo and useCallback?
  • How does the React Compiler change when you write memo by hand?

Tips

  • Explain shallow comparison and Object.is in one sentence. It shows the mechanism.
  • Give the inline-object trap as your example of failure. It is the most common real bug.
  • Say you would profile first. It separates practical engineers from textbook answers.