Skip to the page

Performance · mid

memo, useMemo and useCallback

memo skips re-rendering a component when its props are shallow-equal; useMemo caches a calculated value and useCallback caches a function identity between renders; they only help when the cached identity is actually used for a comparison or the work is expensive.

The notes

What you must know

  • memo(Component): on a parent re-render, React compares old and new props one by one with Object.is; if all equal, it reuses the last result.
  • useMemo(() => compute(a, b), [a, b]): recompute only when deps change. Use for expensive calculations or to keep an object/array identity stable for a memoized child or an effect dep.
  • useCallback(fn, deps) is useMemo(() => fn, deps): same function object between renders. It does not make the function faster.
  • memo fails if any prop is new each render: inline objects (style={{...}}), inline arrows (onSelect={() => ...}), and JSX children.
  • Components that read a context still re-render when it changes, even inside memo.
  • Costs: memory, comparison time and more complex code. Useless if the props change anyway.
  • React may drop a useMemo cache in some cases; treat it as a performance hint, not a guarantee of identity for correctness.
  • With React Compiler 1.0 the compiler adds memoization automatically; for new code rely on it and use manual hooks as escape hatches (for example a stable effect dependency).

The red pen

Common traps: mistake → fix

  • Mistake: "useCallback makes functions faster"

    Fix: it only keeps the same identity.

  • Mistake: "memo the child and the problem is solved"

    Fix: inline object/function/children props break it.

  • Mistake: "Memoize every value"

    Fix: cheap calculations cost more to memoize than to redo.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is the difference between React.memo, useMemo and useCallback?
  2. Implement useCallback using useMemo.
  3. We wrapped Row in memo but every row still re-renders. Why?
  4. When does memoization make things slower?
  5. With the React Compiler enabled, do you still write these?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview