Ankit KanojiyaIntermediate

Evergreen guide by Ankit Kanojiya · Intermediate · Frontend

Taming React Re-renders: memo, useMemo, useCallback, and Keys

When and why React components re-render, and how to apply memoization and stable keys without over-optimizing. Covers the common traps that make performance worse.

React re-renders more often than many developers expect, and most of the time that is perfectly fine. Problems appear only when a re-render is both frequent and expensive. The tools React provides, memo, useMemo, useCallback, and correct keys, are precise instruments, not blanket solutions. Using them well requires understanding why a component rendered in the first place.

Why components re-render

A component function runs again when any of the following happens:

  • Its own state changes.
  • Its parent re-renders, so it is called again with new props.
  • A context it consumes changes.

Note the second point: a child re-renders when its parent does, even if the child's props are unchanged by value. React re-running the function is cheap on its own. The cost comes from large subtrees or heavy work inside the render.

Measure before optimizing

The React DevTools Profiler records which components rendered and how long each took. Before reaching for memoization, confirm there is an actual bottleneck. Premature memoization adds code and dependency arrays that must be maintained, and it can hide real bugs.

React.memo

Wrapping a component in React.memo skips its re-render when its props are shallowly equal to the previous props:

const Row = React.memo(function Row({ label, value }) {
  return <li>{label}: {value}</li>;
});

This helps only when:

  • The component is re-rendered often by its parent.
  • Its props usually stay the same between those renders.
  • Rendering it is genuinely expensive.

The shallow comparison is the catch. If a parent passes a new object or function on every render, the props are never equal and memo does nothing.

useCallback and useMemo

These hooks give you stable references across renders, which is what makes React.memo effective on children that receive functions or objects.

function List({ items }) {
  const handleSelect = useCallback((id) => {
    console.log('selected', id);
  }, []);

  const sorted = useMemo(
    () => [...items].sort((a, b) => a.name.localeCompare(b.name)),
    [items]
  );

  return sorted.map((item) => (
    <Row key={item.id} item={item} onSelect={handleSelect} />
  ));
}
  • useCallback memoizes a function so its identity is stable while its dependencies are unchanged.
  • useMemo memoizes a computed value so expensive work is not repeated on every render.

Two rules keep these honest:

  • The dependency array must list every value used inside. Omitting one causes stale closures.
  • They are caches, not guarantees. React may discard a useMemo value, so never rely on it for correctness, only for performance.

When memoization backfires

Memoization is not free. Each useMemo and useCallback stores a value and runs a comparison. For cheap computations, this overhead can exceed the cost it tries to avoid. Signs of over-optimization include wrapping trivial values, long dependency arrays that change constantly, and memo applied to components whose props always differ anyway.

A cleaner fix is often structural. Moving state down so fewer components depend on it, or lifting expensive children out as props, can eliminate re-renders without any memoization:

// Children passed in do not re-render when Parent's state changes
function Parent({ children }) {
  const [open, setOpen] = useState(false);
  return <div>{children}</div>;
}

Keys and list identity

Keys tell React which list item is which between renders. They are not a performance micro-optimization; they are about correctness. A wrong key causes React to reuse the wrong DOM node and component state.

  • Use a stable, unique identifier from your data, such as a database id.
  • Do not use the array index as a key when the list can reorder, filter, or have items inserted. State and focus will attach to the wrong rows.
  • A key only needs to be unique among its siblings, not globally.
// Fragile when the list reorders
{todos.map((todo, i) => <Todo key={i} todo={todo} />)}

// Stable
{todos.map((todo) => <Todo key={todo.id} todo={todo} />)}

A practical checklist

  • Profile first and confirm the re-render is expensive and frequent.
  • Try restructuring state and composition before memoization.
  • Reach for React.memo on heavy children, and pair it with useCallback and useMemo so their props stay stable.
  • Keep dependency arrays complete and honest.
  • Use stable data-driven keys for lists, never the index for dynamic lists.

The goal is not to eliminate every render but to make sure the renders that do happen are cheap.