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.

🛒
UrbanCart Example
The re-render cascade 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.

Default Re-render Cascade
Parent state changes
e.g. cart count++
→
Parent re-renders
React diffing runs
→
All children re-render
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.

Golden rule: Don't optimize prematurely. Profile first with React DevTools, then apply memoization where it actually matters. Over-memoization adds its own complexity overhead.

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.

ProductCard.jsx Without vs With React.memo
✅

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.

Shallow comparison caveat: React.memo uses shallow equality. If you pass a new object or array as a prop on every render (e.g., 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.

ProductList.jsx useMemo for expensive filter/sort
What useMemo saves you
Without useMemo: Cart click → re-render → filter 500 products again → sort 500 products → 12ms wasted With useMemo: Cart click → re-render → deps unchanged → return cached result → 0ms Search change → re-render → deps changed → recompute → 12ms (necessary)
Syntax reminder: 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.

ProductList.jsx useCallback to stabilize handlers
🧠

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.

App.jsx React.lazy + Suspense for route-level splitting
Why this matters: The Admin Dashboard chunk (often very large) is never downloaded by regular customers. The Checkout chunk only loads when users reach checkout. Initial bundle size drops dramatically, and the homepage loads faster — which directly improves conversion rates and SEO.
Bundle before vs after code splitting
Before (single bundle): main.js → 1.2 MB (everything loaded upfront) After (code splitting): main.js → 180 KB (home + shell) products.js → 95 KB (loaded on /products) checkout.js → 120 KB (loaded on /checkout) admin.js → 410 KB (loaded only by admins) Initial load: 1.2 MB → 180 KB (85% smaller!)
Limitation: 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.

Inline object/array props

Example Inline objects break React.memo

Missing keys in lists

Example Always use stable keys

Unnecessary useEffect dependencies

Example Over-broad useEffect deps
🚫

Other anti-patterns

  • Storing derived state in useState (use useMemo)
  • Creating context value as an inline object
  • Fetching data without cleanup/abort on unmount
🔧

Tools to diagnose

  • React DevTools Profiler — see which components re-render
  • React DevTools Components — inspect props/state live
  • why-did-you-render library — logs unnecessary re-renders

7. Folder Structure Best Practices

Performance is also about developer speed. A well-organized project structure makes it easy to find, change, and test code. The React community has converged on a standard structure for medium-to-large apps.

src/ ├── components/ # Reusable UI components │ ├── ProductCard/ │ │ ├── ProductCard.jsx │ │ ├── ProductCard.module.css │ │ └── ProductCard.test.jsx │ ├── Button.jsx │ └── Modal.jsx │ ├── pages/ # One file per route │ ├── Home.jsx │ ├── Products.jsx │ ├── Cart.jsx │ └── Checkout.jsx │ ├── hooks/ # Custom hooks │ ├── useCart.js │ ├── useProducts.js │ └── useDebounce.js │ ├── context/ # React context providers │ ├── CartContext.jsx │ └── AuthContext.jsx │ ├── utils/ # Pure helper functions │ ├── formatPrice.js │ └── sortProducts.js │ ├── services/ # API calls │ ├── productsApi.js │ └── cartApi.js │ └── App.jsx
Key principle: Co-locate files that change together. A ProductCard folder with its own CSS and test file is easier to maintain than scattered files. When you delete ProductCard/, you delete everything related — no orphaned files left behind.
Don't over-engineer early: For a new project, start flat — put everything in components/. Introduce pages/, hooks/, and context/ folders only when those patterns emerge naturally. Premature structure adds friction without benefit.

8. Try It Yourself — React.memo + useCallback

This demo shows a parent component with a counter. ProductCard is wrapped with React.memo. A render count badge updates every time ProductCard re-renders. Watch what happens with and without memo, and notice how useCallback is essential to make memo effective.

Try It Yourself — React.memo + useCallback Demo
Preview

9. Try It Yourself — useMemo Demo

This demo shows an expensive filter + sort operation on a product list. A timer shows how long computation takes. An unrelated counter triggers re-renders — with useMemo, those re-renders skip the expensive computation entirely.

Try It Yourself — useMemo Computation Cache
Preview

Quiz

Question 1 of 5
What does React.memo do?
Question 2 of 5
When should you use useMemo?
Question 3 of 5
What is the difference between useMemo and useCallback?
Question 4 of 5
What is React.lazy used for?
Question 5 of 5
Which of these is a React performance anti-pattern?
0
/ 5
Great job!

You completed the React Performance quiz.

Next Lesson

Introduction to Node.js →
You've completed React Advanced! Next up: Arc D — Node.js & Express.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

You just mastered React performance.
Now build a career that performs at the highest level.

Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses →