1. How React Re-renders Work
Before you can optimize React, you need to understand what causes it to re-render. The rule is simple: whenever a component's state or props change, React re-renders that component — and all of its children.
This cascading behavior is usually fine for small UIs. But as UrbanCart grows to hundreds of products, deep component trees, and frequent state updates, unnecessary re-renders become a real performance problem.
The UrbanCart header has a cart counter that updates every time a user adds a product. If Header re-renders, React also re-renders SearchBar, Logo, NavLinks, and CartIcon — even though only the counter changed. The SearchBar, Logo, and NavLinks are doing wasted work on every single cart update.
e.g. cart count++
React diffing runs
Even unchanged ones!
React's reconciler is fast, but re-rendering 50 ProductCard components on every keystroke in the search bar is wasteful. The tools in this lesson give you surgical control over when components update.
2. React.memo — Skip Re-renders When Props Are Unchanged
React.memo is a higher-order component that wraps a component and memoizes its output. Before React re-renders the wrapped component, it shallowly compares the new props with the previous props. If they're the same, it skips the re-render entirely and reuses the last rendered output.
When to use React.memo
Pure components that render the same output for the same props. Components that re-render often due to a parent state change they don't care about. Expensive components in long lists.
When NOT to use it
Simple components that are cheap to render. Components whose props change on almost every render anyway. Components with very few renders in total — the comparison overhead isn't worth it.
style={{ color: 'red' }} inline), memo will not prevent re-renders because the object reference is new each time. This is why useCallback and useMemo are often paired with memo.
3. useMemo — Cache Expensive Computed Values
useMemo memoizes the result of a function call. It only recomputes the value when one of its dependencies changes. Between renders, it returns the cached value without running the function again.
useMemo(() => expensiveComputation(), [dep1, dep2]). The first argument is a function that returns the value. The second is the dependency array — same pattern as useEffect.
4. useCallback — Stable Function References
Every time a React component renders, any function defined inside it is recreated as a new object. Even if the function's logic is identical, it has a new reference in memory. This breaks React.memo — if you pass a memo'd child an inline function, the child always sees new props and always re-renders.
useCallback memoizes the function itself, returning the same function reference until its dependencies change.
useMemo vs useCallback
useMemo memoizes a computed value (any type). useCallback memoizes a function. In fact, useCallback(fn, deps) is equivalent to useMemo(() => fn, deps).
When they work together
React.memo + useCallback is the standard trio. Memo stops child re-renders. useCallback keeps the function prop reference stable so memo's comparison actually passes.
5. React.lazy + Suspense — Code Splitting
By default, webpack bundles all your JavaScript into one large file. A customer loading the UrbanCart homepage downloads code for the Admin Dashboard, the Order History page, and the Checkout flow — none of which they need yet. This slows down the initial page load.
Code splitting solves this: split your bundle into chunks and load each chunk only when it's actually needed. React.lazy and Suspense make this simple.
React.lazy only works with default exports. Named exports must be wrapped: lazy(() => import('./Comp').then(m => ({ default: m.NamedExport }))).
6. Common Performance Mistakes
Most React performance bugs come from a handful of patterns. Recognizing them is half the battle.