Performance and best practices
React is fast by default. Most performance problems come from a few avoidable patterns. This lesson explains when components re-render, the tools to control it, and the architecture habits that keep large React apps healthy.
When does a component re-render?
A component re-renders when:
- Its state changes, or
- Its parent re-renders (by default, all children re-render too), or
- A context it uses changes.
Re-rendering isn't bad in itself. React only touches the real DOM where something changed. It becomes a problem only when the rendering work itself is expensive, or happens hundreds of times a second.
memo: skip re-rendering when the props are the same
memo makes SlowList skip renders while its items prop is the same array. Remove memo(...) and typing becomes laggy, and the console fills up.
useMemo and useCallback: keep values stable
memo compares props by reference. A new object or function created on every render looks "different" each time, which defeats memo:
Click "Unrelated state". Results doesn't re-render, because both of its props kept the same reference.
React Compiler: React now has an optimising compiler that adds this memoisation automatically at build time. In projects that use it, you rarely need to write
memo,useMemooruseCallbackby hand. Either way, measure first: use the React DevTools Profiler before optimising.
Bigger wins than memoisation
| Technique | Why it helps |
|---|---|
| Keep state close to where it's used | Typing in a search box shouldn't re-render the entire page |
| Derive values; don't sync state with effects | Fewer renders, fewer bugs |
| Virtualise long lists (TanStack Virtual, react-window) | Render only the 20 visible rows, not 10,000 |
Code-split with lazy() + <Suspense> |
Load heavy pages and components only when needed |
useTransition / useDeferredValue |
Keep typing responsive while an expensive update renders in the background |
| Optimise images, avoid huge bundles | Usually the biggest real-world speed-up |
Project structure that scales
src/
app/ or pages/ routes and screens
components/
ui/ Button, Input, Modal: generic, reusable
product/ ProductCard, ProductGallery: feature-specific
hooks/ useCart, useDebounce
lib/ api.ts, formatters.ts, plain non-React logic
types/Best-practice checklist
- Components are small and do one thing; split anything over ~150 lines
- Props typed with TypeScript
- Stable keys for lists (never index for dynamic lists)
- No mutation of state or props
- Effects only for external sync, each with a cleanup
- Loading, error and empty states handled
- Accessible: semantic HTML, labels, buttons (not clickable divs), keyboard support
- Tested: React Testing Library + Vitest/Jest, testing what the user sees and does
- Linted with
eslint-plugin-react-hooks
🎓 React track complete!
You can build interactive UIs with components, props, state, events, lists, forms, effects, context, reducers and custom hooks, and keep them fast. Next: Next.js, to turn React into full-stack, server-rendered apps.
Practice
Optimise this app so that typing in the input doesn't re-render ExpensiveChart. Use memo and keep its props stable. Check the console to confirm.
1.By default, what happens to child components when a parent re-renders?
2.Why might memo not prevent a re-render even though the "same" data is passed?