React Performance Patterns

A component re-renders when its state changes, its parent re-renders, or its context changes. Measure first with the React DevTools Profiler. React.memo skips…

You type in the search box, and each letter only appears almost 300 ms later. Clicking +1 cart feels heavy too. The cause is the same: the list of 5,000 products below keeps re-rendering, even though its content often hasn't changed.

React performance means getting React to do only the work that's needed: not re-rendering components whose result is the same, and not letting heavy work hold up what the user types. Picture changing one word on page 1 of a book, then the printer reprints all 200 pages. memo is a printer that checks first: "is this page the same as last time? Skip it." But the check also takes time, so it's only worth it for pages that are expensive to print.

When a component re-renders

Rendering means React calls your component function to work out what to show, then changes only the parts of the DOM that differ. A component re-renders when:

  1. its own state changes (useState, useReducer),
  2. its parent re-renders: every child renders too, even with exactly the same props,
  3. a context it reads changes.

"Props changed" isn't a trigger of its own: props can only change because the parent re-rendered. A re-render is usually cheap (1 or 2 ms). It becomes a problem when the component is expensive, a list of 5,000 products for example, and it renders often.

Measure first with the React DevTools Profiler

Don't guess which component is slow. Install the React DevTools extension, open the Profiler tab, press Record, do the interaction that feels slow, then Stop. The flamegraph shows every component that rendered and for how long; grey ones didn't render. In its settings, turn on "Record why each component rendered" so every render comes with its reason.

Here is the result from the Lab on this page: a shop with a search box, a +1 cart button, and a ProductList of 5,000 products. One list render takes about 280 ms (simulated in the Lab, it doesn't really burn the CPU).

Interaction App renders ProductList renders ProductList props changed?
Type "k" 1 1 yes, query "" became "k"
Type "o" 1 1 yes, "k" became "ko"
Click +1 cart 8 times 8 8 no
Total 10 10 2 out of 10, so 20%

Eight of those list renders were wasted: 8 × 280 ms = 2,240 ms of work with no different result.

React.memo: skip the render when props are the same

import { memo, useState } from 'react';

const ProductList = memo(function ProductList({ query }) {
  const visible = products.filter((p) => p.name.includes(query)); // 5,000 products, expensive
  return (
    <ul>
      {visible.map((p) => <li key={p.id}>{p.name}</li>)}
    </ul>
  );
});

function App() {
  const [query, setQuery] = useState('');
  const [cart, setCart] = useState(0);
  return (
    <>
      <input value={query} onChange={(e) => setQuery(e.target.value)} />
      <button onClick={() => setCart(cart + 1)}>+1 cart ({cart})</button>
      <ProductList query={query} />
    </>
  );
}

useCallback and useMemo: keep props the same

Before you go on, guess: ProductList is wrapped in memo, but App passes onSelect={() => pick(id)} written straight in the JSX. When you click +1 cart, does ProductList re-render or not?

It still re-renders. () => pick(id) written straight in the JSX is a new function on every render, so memo sees the onSelect prop change. The same goes for an object or array made inside render.

const handleSelect = useCallback((id) => setSelected(id), []); // the same function across renders
const sorted = useMemo(() => [...products].sort(byPrice), [products]); // recomputed when products changes

<ProductList query={query} items={sorted} onSelect={handleSelect} />

In short: React.memo for components, useMemo for values, useCallback for functions.

Keep the input responsive: useDeferredValue and useTransition

Without a trick, every keystroke waits for the 280 ms list render to finish, so the delay from typing to the letter appearing is about 290 ms. The good INP limit is 200 ms.

const deferredQuery = useDeferredValue(query);
<ProductList query={deferredQuery} /> // ProductList stays memo

React now renders twice per keystroke. The first render is urgent: the input gets the new letter, deferredQuery still holds the old value, and memo skips ProductList. The second render runs in the background with the new value, and it can be interrupted if you type again.

Mode in the Lab List renders per keystroke Input delay
Nothing 1, in the urgent render about 290 ms
useDeferredValue without memo 2, old value and new about 290 ms
useDeferredValue + memo 1, in the background about 12 ms

When you call the setState yourself, use useTransition: startTransition(() => setFilter(value)) marks that update as not urgent, and isPending can drive a loading indicator.

Other patterns that often help

The exercise version: when memo pays off

shouldMemoize returns true only when all three conditions hold: renderCount > 5 (renders often), propsChangedPercent < 30 (props rarely change, so memo can often skip), and computationMs > 10 (each render is expensive).

renderCount propsChangedPercent computationMs Result
10 20 15 true
3 20 15 false, rarely renders
10 50 15 false, props change often
10 20 5 false, the render is cheap

identifyUnneededReRenders keeps the components with propsChanged === false: the ones that rendered without their props changing, candidates for React.memo. From Header, UserCard and Footer, the result is Header and Footer.

⚠️ Memo that's wasted, or missing

TL;DR: A component re-renders when its state changes, its parent re-renders, or its context changes. Measure first with the React DevTools Profiler. React.memo skips the render when props are the same, useCallback keeps a function the same, useMemo keeps a value. A new function, object or array on every render defeats memo. useDeferredValue (or useTransition) plus memo keeps the input responsive while a heavy list catches up in the background. Use stable keys, keep state near its users, virtualise long lists, and consider React Compiler. Memo pays off when a component renders often, its props rarely change, and its render is expensive.

⚡ When a component re-renders

Meaning: A component re-renders when its state changes, its parent re-renders, or a context it reads changes. A re-render is usually cheap, but it becomes a problem when the component is expensive and renders often. Measure first with the React DevTools Profiler.

⚡ memo skips a render that's the same

Write:

const ProductList = memo(function ProductList({ query }) {
  const visible = products.filter((p) => p.name.includes(query));
  return <ul>{visible.map((p) => <li key={p.id}>{p.name}</li>)}</ul>;
});
Guess first: React.memo, useMemo and useCallback sound alike. What's the difference?

React.memo is for components, useMemo for values, useCallback for function references. A new function, object or array on every render means memo never skips.

Meaning: In the Lab, clicking +1 cart no longer re-renders the list of 5,000 products: from 10 renders down to 2.

⚡ Keep the input responsive

Write:

const deferredQuery = useDeferredValue(query);
<ProductList query={deferredQuery} />

Meaning: The input gets its new letter first, and the heavy list catches up in the background. Together with memo, the typing delay drops from about 290 ms to about 12 ms.

What you will learn