TExcelTAD
Home › React › React memo

React memo

When a parent re-renders, React re-renders all of its children by default — even those whose props didn't change. That's usually fast enough. But if a child is expensive to render, you can wrap it in memo so that React skips it when its props are the same as last time.

jsx
import { memo } from 'react';

const ProductCard = memo(function ProductCard({ product }) {
  return <div>{product.name}</div>;
});

Seeing the difference

Both lists below render the same rows. Type in the input to re-render the parent and watch the render counters:

jsx · try it yourself
import { memo, useState } from 'react';

const counts = {};

function Row({ kind, label }) {
  const id = `${kind}-${label}`;
  counts[id] = (counts[id] ?? 0) + 1;
  return <li>{label} <small style={{ color: 'gray' }}>(rendered {counts[id]}×)</small></li>;
}

const MemoRow = memo(Row);

export default function App() {
  const [text, setText] = useState('');
  return (
    <div>
      <input value={text} onChange={(e) => setText(e.target.value)} placeholder="Type to re-render App" />
      <p>Without memo:</p>
      <ul><Row kind="plain" label="Apples" /><Row kind="plain" label="Pears" /></ul>
      <p>With memo:</p>
      <ul><MemoRow kind="memo" label="Apples" /><MemoRow kind="memo" label="Pears" /></ul>
    </div>
  );
}

The plain rows re-render on every keystroke; the memoized rows render once and are then skipped, because kind and label never change. (The module-level counter only exists to visualize renders.)

How memo compares props

memo compares each prop with its previous value using Object.is — a shallow comparison:

Prop Same between renders?
label="Apples" (string literal) ✅ yes
count={3} (number) ✅ yes
style={{ color: 'red' }} (new object each render) ❌ no
items={[1, 2]} (new array each render) ❌ no
onClick={() => …} (new function each render) ❌ no
a state variable that didn't change ✅ yes

A single new object or function prop makes memo useless. That's why memo is often paired with useMemo and useCallback:

jsx · try it yourself
import { memo, useCallback, useMemo, useState } from 'react';

let renders = 0;

const Chart = memo(function Chart({ data, options, onPointClick }) {
  renders += 1;
  // pretend this is an expensive chart
  return (
    <div>
      <div style={{ display: 'flex', alignItems: 'flex-end', gap: 4, height: 80 }}>
        {data.map((v, i) => (
          <div key={i} onClick={() => onPointClick(i)} style={{ width: 24, height: v, background: options.color, cursor: 'pointer' }} />
        ))}
      </div>
      <small>Chart rendered {renders}×</small>
    </div>
  );
});

export default function App() {
  const [note, setNote] = useState('');
  const [color, setColor] = useState('#61dafb');
  const [picked, setPicked] = useState(null);

  const data = useMemo(() => [30, 60, 45, 80, 20], []);
  const options = useMemo(() => ({ color }), [color]);
  const onPointClick = useCallback((i) => setPicked(i), []);

  return (
    <div>
      <input value={note} onChange={(e) => setNote(e.target.value)} placeholder="Typing won't re-render the chart" />{' '}
      <input type="color" value={color} onChange={(e) => setColor(e.target.value)} />
      <Chart data={data} options={options} onPointClick={onPointClick} />
      <p>{picked === null ? 'Click a bar' : `You picked bar #${picked + 1}`}</p>
    </div>
  );
}

Typing a note leaves the chart alone; changing the colour creates a new options object, so the chart correctly re-renders.

What memo doesn't stop

memo only looks at props. A memoized component still re-renders when:

  • its own state changes,
  • a context it reads with useContext changes,
  • its parent passes a prop that is a new object, array or function.

Custom comparison

You can pass a second argument (prevProps, nextProps) => boolean that returns true when the props should be treated as equal. Use it rarely — it's easy to get wrong and to skip renders you actually needed:

jsx
const Avatar = memo(
  function Avatar({ user }) { return <img src={user.photo} alt={user.name} />; },
  (prev, next) => prev.user.id === next.user.id && prev.user.photo === next.user.photo
);

Often better than memo

Before adding memo, try restructuring so fewer components re-render at all:

jsx · try it yourself
import { useState } from 'react';

let slowRenders = 0;
function SlowList() {
  slowRenders += 1;
  return <p>📋 A slow list (rendered {slowRenders}×)</p>;
}

// State moved down into its own small component:
function NoteInput() {
  const [note, setNote] = useState('');
  return <input value={note} onChange={(e) => setNote(e.target.value)} placeholder="Typing only re-renders NoteInput" />;
}

export default function App() {
  return (
    <div>
      <NoteInput />
      <SlowList />
    </div>
  );
}

By moving state down into NoteInput, typing no longer re-renders App, so SlowList is never touched — no memo required. Passing expensive parts as children achieves the same effect.

Technique When
Move state down only a small part of the UI uses the state
Pass JSX as children a wrapper owns state but its content doesn't depend on it
memo an expensive component re-renders often with the same props
React Compiler automates memoization across the whole app

Note: With the React Compiler enabled, components and values are memoized automatically at build time, and manual memo becomes mostly unnecessary. Understanding it still helps you reason about re-renders.

Test yourself

When does a component wrapped in memo skip re-rendering?

  1. Always
  2. When its props are shallowly equal to the previous props
  3. When it has no state
  4. When its parent is a class

A memo child receives style={{ margin: 0 }} from its parent. What happens on each parent render?

  1. The child is skipped
  2. The child re-renders because the object is new each time
  3. React throws an error
  4. The style is ignored

Which of these still re-renders a memoized component even with identical props?

  1. Nothing can
  2. A change in a context it reads
  3. A sibling's state change
  4. A CSS change