0
Challenge · 33Hooks & Effects
useMemo
Performanceused in ~75% of apps
Your component body re-runs top-to-bottom on every render. If one line is an expensive sort over 10,000 rows — or builds a fresh object that breaks a memo'd child — you pay for it on renders that changed nothing. useMemo is the cache that lets you skip the work.
Core idea. useMemo(() => compute(a, b), [a, b]) caches a computed VALUE between renders and only recomputes it when a dependency changes — so expensive work is skipped, and the returned reference stays stable.
poke it, drag it,
try to break it
try to break it
no slides here.
you drive.
you drive.
Playground, this isn't a screenshot
useMemo
renders1
computed1
const total =
useMemo(() => slowSum(n), [n])
n = 3→total = 6
render log (newest first)
r1first render (mount)RAN slowSum
With useMemo on, an unrelated re-render skips the calc — slowSum only re-runs when [n] changes. The cache hits.
↑ play a little first, then dig in