Lazy Loading — Complete Guide
Lazy Loading — 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 64 of 100
Code Splitting
Beginner ✓ → Intermediate ✓ → Advanced → Professional
Advanced · 3 — Production skills · ~18 min read · Module 7: Performance & Scaling
Introduction
This is advanced material: Code Splitting. It is what teams use on live products. Read the example carefully and try changing one line at a time to see what happens. Code splitting breaks your JavaScript bundle into smaller files loaded on demand. Vite and webpack do this automatically with dynamic import(). One 2MB bundle blocks first load. Splitting lets users download only what they need for the current page.
Do not optimize early. First make it work, then measure, then fix what is actually slow.
When will you use this?
Apply when users complain the app feels slow, or Lighthouse shows red scores.
- Slow admin tables with thousands of rows need memo, lazy load, or virtualization.
- Lighthouse scores matter for public sites — clients notice slow pages.
Real-world: route-based chunks
Vite + React Router emit separate chunks per major route — home visitors never download admin bundle.
Production-style code
const routes = [
{ path: '/', lazy: () => import('./pages/Home.jsx') },
{ path: '/shop', lazy: () => import('./pages/Shop.jsx') },
{ path: '/admin', lazy: () => import('./pages/Admin.jsx') }
];
// vite build output (example):
// dist/assets/Home-a1b2.js 45 kB
// dist/assets/Admin-c3d4.js 380 kB
What happens in production: First contentful paint improves — split by route and by heavy third-party libraries.
Lesson example (start here)
Copy this smaller example first. Once it works, compare it with the real-world code above.
// Vite creates separate chunk automatically:
const Settings = lazy(() => import('./pages/Settings'));
Line-by-line walkthrough
| Code | What it means |
|---|---|
// Vite creates separate chunk automatically: | Comment — notes for humans; the computer ignores it. |
const Settings = lazy(() => import('./pages/Settings')); | Defines a function — often a component or event handler. |
How it works (big picture)
- Static import bundles together.
- import() creates a separate file fetched at runtime when the lazy component renders.
Do this on your computer
- Run npm run build and check dist/assets chunk sizes.
- Lazy load large routes.
- Analyze with rollup-plugin-visualizer or vite-bundle-visualizer.
- 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 Code Splitting and inspect component props/state.
Remember
dynamic import = automatic split. Lazy routes are the main win. Analyze production build.
Common questions
Vite splits automatically?
Yes for dynamic imports; entry chunk still needs route-level lazy loads.
How long should I spend on Code Splitting?
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 Code Splitting?
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 Code Splitting 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!