React.memo — Complete Guide
React.memo — 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 63 of 100
Lazy Loading
Beginner ✓ → Intermediate ✓ → Advanced → Professional
Advanced · 3 — Production skills · ~18 min read · Module 7: Performance & Scaling
Introduction
This is advanced material: Lazy Loading. It is what teams use on live products. Read the example carefully and try changing one line at a time to see what happens. Lazy loading defers loading code or content until needed — React.lazy for components, loading images when they scroll into view. Users should not download code for admin pages they never visit, or images below the fold immediately.
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: PDF viewer on demand
Invoice page loads lightweight summary first; PDF viewer chunk downloads only when user clicks "View PDF".
Production-style code
const PdfViewer = lazy(() => import('./PdfViewer.jsx'));
function InvoicePage({ invoiceId }) {
const [showPdf, setShowPdf] = useState(false);
return (
<>
<InvoiceSummary id={invoiceId} />
<button onClick={() => setShowPdf(true)}>View PDF</button>
{showPdf && (
<Suspense fallback={<p>Loading viewer…</p>}>
<PdfViewer url={`/api/invoices/${invoiceId}/pdf`} />
</Suspense>
)}
</>
);
}
What happens in production: Initial page weight drops — users who never open PDF never download the library.
Lesson example (start here)
Copy this smaller example first. Once it works, compare it with the real-world code above.
const Chart = lazy(() => import('./Chart'));
{showChart && (
<Suspense fallback={<Spinner />}>
<Chart data={data} />
</Suspense>
)}
Line-by-line walkthrough
| Code | What it means |
|---|---|
const Chart = lazy(() => import('./Chart')); | Defines a function — often a component or event handler. |
{showChart && ( | Part of the Lazy Loading example — read it together with the lines before and after. |
<Suspense fallback={<Spinner />}> | JSX tag — a UI element or custom component on the page. |
<Chart data={data} /> | JSX tag — a UI element or custom component on the page. |
</Suspense> | Part of the Lazy Loading example — read it together with the lines before and after. |
)} | Closes a block started by { or ( above. |
How it works (big picture)
- Chart.js loads only when showChart is true.
- Combine with Suspense for loading state.
Do this on your computer
- Lazy load heavy routes and charts.
- Use loading="lazy" on img tags below fold.
- Measure bundle size before/after.
- 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 Loading and inspect component props/state.
Remember
React.lazy for components. Native lazy for images. Load on demand.
Common questions
Lazy load images in React?
Use loading="lazy" on img or Intersection Observer for custom components.
How long should I spend on Lazy Loading?
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 Loading?
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 Loading 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!