its parent renders it again, even if the props have the same values;
a context it reads changes.
React can skip work when the state is unchanged, when it reuses the same JSX
element, or when a memoization optimization applies. A render isn't a promise
that the DOM will change.
Rule 2 surprises people. Re-rendering doesn't mean the page changes. React
calls the function, compares the result with the last one, and only touches
the DOM where something differs. For most components, that's fast enough to
ignore. Today is about the cases where it isn't.
memo makes React compare the new props with the old ones, using Object.is
for each. If they're all the same, it reuses the last result and skips the
render.
Predict
With memo in place, click Tick. Does CardList rendered still appear?
Check your prediction
Yes, every time. cards is the same array each render, but onSelect isn't:
(id) => setSelectedId(id) is a new function every time App renders. To
memo, a new function is a changed prop.
Stable functions
Two fixes:
Pass something that's already stable. State setters never change, so
onSelect={setSelectedId} is the same function every render.
useCallback keeps the same function between renders until its
dependencies change:
Use the first fix here. Now Tick logs nothing. Selecting a card still
works, and doesn't re-render CardList either, because its props didn't
change.
Measure, then useMemo
useMemo does for a value what memo does for a component: it keeps the
last result until its dependencies change. Before reaching for it, measure.
This lab's slowTotal stands in for an expensive calculation and logs how long
it took:
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Click Dark mode a few times. Every click logs slowTotal took 100 ms
(give or take a millisecond), and the button feels sticky. That log is your
measurement: the total is recalculated on every render, although switching the
theme can't change it.
Now keep the result between renders, recalculating only when cards changes:
JavaScript
const total = useMemo(() => slowTotal(cards), [cards]);
Import useMemo from "react". Predict which buttons log a time now, then
click both.
Check your prediction
Dark mode logs nothing: cards is the same array, so React hands back the
last total without calling slowTotal. Add card logs once, because the new
cards array is a changed dependency, and the total really does need
recalculating.
A console.log timer is enough for one calculation. For a whole app, the
React DevTools browser extension has a Profiler tab: record while you
click, and it lists every component that rendered and how long each took. Look
at the numbers before and after a memo or useMemo. If they don't drop,
take it out again.
useMemo also keeps a memoized child's props stable:
useMemo(() => cards.filter(...), [cards]) hands a memo child the same array
until cards changes.
Don't memo everything
memo, useCallback and useMemo cost a comparison on every render, and they
make code harder to read. Use them when you've measured a problem: a slow
component, or one that re-renders very often for no reason. React's own
compiler, when you use it, adds this memoization for you, so writing it by hand
is becoming less necessary.
Exercise
Keep an inline handler
Suppose App needs to log each selection, so it can't just pass
setSelectedId. Keep onSelect an inline-looking handler that logs
selected 3 and sets the state, without making CardList re-render on
Tick.