Lazy Routes — Complete Guide
Lazy Routes — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of React.js Tutorial on Toolliyo Academy.
On this page
React.js Tutorial · Lesson 34 of 100
Lazy Routes
Beginner ✓ → Intermediate → Advanced → Professional
Intermediate · 2 — Build apps · ~14 min read · Module 4: Routing & Data
Introduction
You know the basics now. Here we use Lazy Routes in real app situations — forms, pages, and data. Still plain language, just a bit more depth. Lazy routes download a page’s JavaScript only when the user opens that URL. You use React.lazy() to import the file and Suspense for a loading fallback. The first page load stays small. Users who never visit Admin never download the Admin bundle.
Routing is how a single React app feels like many pages. Users expect URLs like /cart and /checkout — Router makes that work.
When will you use this?
Add routing when your app has more than one screen and URLs should be bookmarkable.
- Multi-page apps like dashboards use React Router — /settings, /profile, /orders.
- E-commerce sites route /product/123 to a product detail component.
Real-world: load admin bundle on demand
99% of Flipkart shoppers never open seller admin. lazy() loads the heavy admin chunk only when URL matches /seller — first visit stays fast.
Production-style code
import { lazy, Suspense } from 'react';
const SellerAdmin = lazy(() => import('./pages/SellerAdmin.jsx'));
const ShopperHome = lazy(() => import('./pages/ShopperHome.jsx'));
function App() {
return (
<Suspense fallback={<PageSkeleton />}>
<Routes>
<Route path="/" element={<ShopperHome />} />
<Route path="/seller/*" element={<SellerAdmin />} />
</Routes>
</Suspense>
);
}
What happens in production: Lighthouse bundle size drops for shoppers; sellers pay one-time load cost when entering dashboard — fair trade-off.
Lesson example (start here)
Copy this smaller example first. Once it works, compare it with the real-world code above.
import { lazy, Suspense } from 'react';
const Admin = lazy(() => import('./pages/Admin'));
<Route path="/admin" element={
<Suspense fallback={<p>Loading page...</p>}>
<Admin />
</Suspense>
} />
Line-by-line walkthrough
| Code | What it means |
|---|---|
import { lazy, Suspense } from 'react'; | Imports code from React or another file so you can use it here. |
const Admin = lazy(() => import('./pages/Admin')); | Defines a function — often a component or event handler. |
<Route path="/admin" element={ | React Router — maps URLs to page components. |
<Suspense fallback={<p>Loading page...</p>}> | JSX tag — a UI element or custom component on the page. |
<Admin /> | JSX tag — a UI element or custom component on the page. |
</Suspense> | Part of the Lazy Routes example — read it together with the lines before and after. |
} /> | Closes a block started by { or ( above. |
How it works (big picture)
- import() returns a promise.
- React.lazy wraps it as a component.
- Suspense shows fallback while the chunk loads.
Do this on your computer
- Change a heavy page to lazy(() => import(...)).
- Wrap the route element in Suspense.
- Open Network tab and see the chunk load on first visit.
- Read the real-world section and name which part of the app uses this topic.
- Run the example locally and confirm the same behavior in the browser.
- Change one value in the example (text, initial state, or URL) and predict what will happen before you save.
Experiments — try changing this
- Change text or labels in the example and save — watch the browser update.
- Break the code on purpose (remove a bracket), read the error message, then fix it.
- Open React DevTools (browser extension) while running Lazy Routes and inspect component props/state.
Remember
React.lazy + dynamic import = code split by route. Suspense shows loading UI. Use for large or rarely visited pages.
Common questions
Does lazy work with named exports?
Use .then(m => ({ default: m.Named })) or export default from the page file.
How long should I spend on Lazy Routes?
Until you can explain it in your own words and run the example without looking at the answer. Beginners often need 30–60 minutes per new hook or routing topic; setup lessons may take one afternoon.
What if I get stuck on Lazy Routes?
Re-read the line-by-line walkthrough, check the browser console for red errors, and compare your code character-by-character with the example. Search the exact error text — someone else had it too.
Where is Lazy Routes used in real jobs?
See the real-world section above — the same pattern appears in LMS, banking, e-commerce, and SaaS products. Interviewers ask you to explain it using one concrete example from your project or this lesson.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!