Lesson 2.2

memo, useMemo, and useCallback

memo skips re-rendering a component when its props are unchanged.

26mAdvanced7.5k students

Overview

Three tools, three different jobs

memo skips re-rendering a component when its props are unchanged. useMemo caches a computed value between renders. useCallback caches a function identity so a memoised child is not defeated by a new prop every time.

They only pay off in specific conditions: the component must be expensive to render or the value expensive to compute, and the memoised props must genuinely stay stable. Wrapping a cheap component whose props change every render adds a comparison and saves nothing.

The dependency array is a contract. Omitting a dependency to stop something recomputing does not fix the render — it produces a stale value that will be wrong in a way that is very hard to trace.

In this lesson you will:

  • Know what each one actually caches
  • Avoid memoisation that cannot possibly help
  • Keep dependency arrays honest

Resources

Previous Lesson
Next Lesson
memo, useMemo, and useCallback — React Performance Engineering — Vertex