How-To Guide

How to Reduce React Re-renders

Find what is re-rendering, why, and fix it by memoising props, moving state down and keeping context from fanning out.

On this page

  1. Introduction
  2. Why React Re-renders
  3. Finding the Re-render
  4. Memoizing Props and Callbacks
  5. State Placement
  6. List Keys and Reconciliation
  7. Context Re-renders
  8. Verifying the Fix
  9. Conclusion

Introduction

React re-renders a component when its state or its props change, and it re-renders its children by default. That is correct and usually cheap, but a component that re-renders an expensive subtree on every keystroke will feel slow. The fix is not to wrap everything in memo; it is to find which component re-renders unnecessarily, why, and correct the cause: an object prop recreated each render, a callback with a new identity, state that lives too high, or a context that changes for unrelated consumers. This article covers how to find the re-render and how to fix each cause.

Why React Re-renders

A component re-renders when its own state changes, when its parent re-renders and passes new props, or when a context it consumes changes. It is normal and not a bug. The problem is re-rendering work that produces the same output, which wastes CPU and can cause a visible jank when the subtree is large or the render is expensive.

The default behaviour is to re-render children whenever the parent does, regardless of whether the props changed. That is intentional: React cannot know whether a child is pure, so it assumes it is not. Memoisation is the opt-out, and it is worth applying only where the render is measurably expensive.

Finding the Re-render

Start by measuring, not guessing. The React DevTools profiler records a session and shows which components rendered, how long each took, and why the render was triggered. Look for components that render often but with unchanged props, and for the commit that took the longest. Only then decide what to memoise.

// A quick, dependency-free way to see which components re-render and why.
import { useEffect, useRef } from 'react';

export function useWhyDidYouRender(name: string, props: Record<string, unknown>) {
  const prev = useRef<Record<string, unknown>>({});

  useEffect(() => {
    const changed: Record<string, { from: unknown; to: unknown }> = {};
    for (const key of Object.keys(props)) {
      if (!Object.is(prev.current[key], props[key])) {
        changed[key] = { from: prev.current[key], to: props[key] };
      }
    }
    if (Object.keys(changed).length > 0) {
      console.log('[re-render]', name, changed);
    }
    prev.current = props;
  });
}

Memoizing Props and Callbacks

Two props break memoisation more than any others: an object or array literal created inline, and a callback created inline. Each render makes a new identity, so a memoised child sees changed props and re-renders anyway. Wrap the object in useMemo and the callback in useCallback so the identity is stable across renders when the inputs have not changed.

import { memo, useCallback, useMemo } from 'react';

// Stable identity across renders: the child stays memoised.
function Parent({ userId }: { userId: string }) {
  const options = useMemo(() => ({ pageSize: 20, sort: 'name' as const }), []);
  const onSelect = useCallback((id: string) => selectUser(id), []);
  return <ExpensiveList userId={userId} options={options} onSelect={onSelect} />;
}

const ExpensiveList = memo(function ExpensiveList(props: Props) {
  // Renders only when userId, options or onSelect actually change.
  return /* ... */ null;
});

State Placement

State that lives higher than it needs to makes everything below it re-render. Moving state down to the component that owns it, or lifting only the leaf that reads it, is often a bigger fix than any memo. A form input whose value lives at the page root re-renders the whole page on every keystroke; moving that state into the input’s component confines the re-render to the input.

The children-as-props pattern helps when a parent must re-render frequently but its subtree must not: pass the expensive subtree as children, and React keeps the same element when the parent re-renders, so the subtree does not re-render. This is the same idea as lifting state down, expressed structurally.

List Keys and Reconciliation

A list keyed by index re-renders and remounts items when the list changes order, because React matches items by position. A stable key from the item’s identity lets React reuse the components and move them instead of recreating them. Using an index as a key also breaks any local state in the item, because the component is reused for a different item.

Make the key stable and unique to the item, never the array index, unless the list is static and never reordered. The cost of a wrong key is subtle: it looks like a performance problem but is really a correctness problem with the component instance.

Context Re-renders

A context re-renders every consumer when its value changes. If one context holds many unrelated values, a change to any one re-renders all consumers. Split the context by change frequency: a rarely changing value in one context and a frequently changing one in another, so a consumer of the stable value does not re-render when the volatile value changes.

A common case is a context whose value is an object literal created each render, which changes every render and re-renders every consumer. Memoise the context value so its identity is stable, and split the provider so a consumer subscribes only to what it reads. This is the same identity problem as props, applied to context.

Verifying the Fix

Verify a change with the profiler, not with intuition. Record a session before the change, reproduce the interaction that felt slow, and note which components committed and how long each took. Apply one change at a time, then record the same interaction again and check that the targeted component no longer renders while the ones you left alone are unchanged. A fix that removes one expensive render but adds a cheaper one elsewhere shows up as a smaller but still changed commit, so compare the flame chart, not just the total time. If the profiler still shows the component rendering with identical props, the identity problem is not fixed: an inline literal or a new callback is still defeating memoisation, and you should keep searching rather than reach for another memo.

Conclusion

Reduce React re-renders by measuring first, then fixing the cause: stabilise object and callback props with useMemo and useCallback, move state down so a frequent change does not re-render a large subtree, use stable list keys so React reuses components, and split context so a change to one value does not fan out to unrelated consumers. Memo only where the render is expensive, because memoisation has its own cost of comparing props and holding the result. Re-measure after each change, because a fix that helps one path can move the bottleneck elsewhere, and the profiler is the only honest judge of whether the render work actually fell. Then the render work scales with what actually changed, not with the size of the tree.

Sources: React: memo, React: useMemo, React DevTools Profiler.

FAQ

Is wrapping every component in memo a good idea?
No. Each memo adds a comparison and a memory cost, and most components are cheap to re-render. Memoise where the render is measurably expensive, and fix the identity problems that defeat memoisation instead of memoising everything.
Why does an inline object prop break memoisation?
Because a literal creates a new object on every render, so the child sees a new prop identity and re-renders even when the contents are equal. Wrap it in useMemo so the identity is stable while the inputs are unchanged.

Related Examples