Skip to main content
14

When does React re-render, and when do you really need useMemo and useCallback?

MediumReactPerformanceMemoization

When does a React component re-render, and when do you actually need useMemo, useCallback and React.memo?

TL;DR

A component re-renders when its state changes, when its parent re-renders, or when a context it reads changes. Props changing isn't the trigger by itself; a parent re-render re-renders all its children by default. Re-rendering is usually cheap. Memoize only when a measured render is slow: React.memo skips a child whose props are shallow-equal, and useMemo/useCallback keep values and functions referentially stable so that memo (or an effect's dependencies) can work. With the React Compiler enabled, most of this happens automatically.

Key points

  • useMemo(fn, deps) caches a computed value; useCallback(fn, deps) caches a function. useCallback(fn, deps) equals useMemo(() => fn, deps).
  • useCallback alone does nothing for performance. It only helps when the function goes to a memo child or into dependencies.
  • Inline objects and arrows (style={{...}}, onClick={() => ...}) create new references every render and defeat memo.
  • Cheaper fixes first: move state down to where it's used, or pass children as props so they don't re-render with the parent.
  • Measure with the React DevTools Profiler before and after.

Example

jsx
const Row = React.memo(function Row({ item, onSelect }) {
  return <li onClick={() => onSelect(item.id)}>{item.name}</li>
})

function List({ items, query }) {
  const visible = useMemo(() => items.filter((i) => i.name.includes(query)), [items, query])
  const onSelect = useCallback((id) => console.log(id), [])
  return (
    <ul>
      {visible.map((i) => (
        <Row key={i.id} item={i} onSelect={onSelect} />
      ))}
    </ul>
  )
}

Common follow-ups

  • What does the React Compiler change? It auto-memoizes components and values at build time, so manual useMemo/useCallback are rarely needed. You still need to understand re-renders to debug.
  • Why does a context update re-render so much? Every consumer re-renders when the value changes. Split contexts or memoize the value object.

What interviewers look for

Tip

The three re-render triggers, "memoize after measuring", and why useCallback only matters with memo or dependencies.

  • Red flag: "wrap everything in useMemo" as a default.

Was this helpful?

Related questions