React is fast by default, but as apps grow you'll occasionally hit needless re-renders or expensive recalculations. React gives you three memoisation tools — React.memo, useMemo, useCallback. The skill is using them deliberately, because used everywhere they add complexity for no gain.
What causes re-renders
A component re-renders when its state changes, or when its parent re-renders. By default a parent re-render re-renders all its children — usually fine, occasionally wasteful for expensive children.
React.memo
React.memo skips re-rendering a component if its props haven't changed (shallow comparison) — useful for an expensive child whose parent re-renders often:
const ExpensiveList = React.memo(function ExpensiveList({ items }) {// only re-renders when 'items' actually changes referencereturn <ul>{items.map(i => <li key={i.id}>{i.name}</li>)}</ul>;});
useMemo
useMemo caches the result of an expensive calculation, recomputing only when its dependencies change:
const sorted = useMemo(() => bigList.slice().sort((a, b) => b.score - a.score),[bigList] // recompute only when bigList changes);
useCallback
Functions are recreated on every render, which can break React.memo (a new function is a new prop). useCallback keeps the same function reference between renders:
const handleSelect = useCallback((id) => {setSelected(id);}, []); // same reference across renders → memoised children don't re-render// useCallback(fn, deps) is essentially useMemo(() => fn, deps).
When to optimise
Practice
Build a list that re-renders when a parent counter ticks, confirm the children re-render needlessly (log in render), then wrap the child in React.memo and stabilise its callback with useCallback. Use useMemo for a derived sorted array. Verify the difference in the Profiler.
Key takeaways
- 01A component re-renders when its state changes or its parent re-renders.
- 02React.memo skips a re-render when props are unchanged (shallow compare).
- 03useMemo caches an expensive computed value; recompute only when deps change.
- 04useCallback keeps a function's reference stable so it doesn't break memoised children.
- 05Measure with the Profiler and optimise deliberately — don't memoise everything.
