Lesson 3 of 10
Code Splitting & Lazy Loading
The largest React performance win for most apps isn't memoization - it's shipping less JavaScript. Code splitting with React.lazy and Suspense defers loading heavy components until they're needed, dramatically improving initial load time.
React.lazy + Suspense - Route & Component Splitting
import { lazy, Suspense } from 'react';
import { Routes, Route } from 'react-router-dom';
// Route-based code splitting - most impactful, minimal effort
// Each lazy() import creates a separate JavaScript chunk
const Dashboard = lazy(() => import('./pages/Dashboard'));
const Analytics = lazy(() => import('./pages/Analytics'));
const Settings = lazy(() => import('./pages/Settings'));
const AdminPanel = lazy(() => import('./pages/AdminPanel')); // large admin bundle
// Preload hint - load AdminPanel while user is on Dashboard
// (before they click the Admin link)
function prefetchAdminPanel() {
import('./pages/AdminPanel'); // trigger the fetch
}
function App() {
return (
<Suspense fallback={<PageLoadingSpinner />}>
<Routes>
<Route path="/dashboard" element={<Dashboard />} onMouseEnter={prefetchAdminPanel} />
<Route path="/analytics" element={<Analytics />} />
<Route path="/settings" element={<Settings />} />
<Route path="/admin" element={<AdminPanel />} />
</Routes>
</Suspense>
);
}
// Component-level splitting - for heavy components that aren't always shown
const RichTextEditor = lazy(() =>
import('./RichTextEditor') // 200KB, only needed when editing
);
const DataGrid = lazy(() =>
import('./DataGrid').then(module => ({ default: module.DataGrid }))
// Handle named exports
);
function ContentPage({ isEditing }: { isEditing: boolean }) {
return (
<div>
<article>...</article>
{isEditing && (
<Suspense fallback={<div>Loading editor...</div>}>
<RichTextEditor />
</Suspense>
)}
</div>
);
}
// Multiple Suspense boundaries = granular fallbacks
function Dashboard() {
return (
<div>
<Suspense fallback={<ChartSkeleton />}>
<SalesChart /> {/* loads independently */}
</Suspense>
<Suspense fallback={<TableSkeleton />}>
<DataTable /> {/* loads independently */}
</Suspense>
</div>
);
}Common Mistakes - Code Splitting
- Not analyzing your bundle first - use `rollup-plugin-visualizer` or webpack-bundle-analyzer to find what's actually large before deciding what to split
- Splitting too aggressively - splitting a 5KB component costs more in HTTP requests and waterfall delays than it saves; split chunks that are >50KB and conditionally rendered
- Missing Suspense boundary - `React.lazy` without `<Suspense>` throws an error; every lazy component needs at least one Suspense ancestor
- Lazy imports inside components - `const Cmp = lazy(() => import('./Cmp'))` inside a component function creates a new lazy component instance every render, causing it to remount; always declare lazy at module level
Tip
Tip
Practice Code Splitting Lazy Loading in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and flow for Code Splitting & Lazy Loading
Practice Task
Note
Practice Task - (1) Write a working example of Code Splitting Lazy Loading from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Code Splitting Lazy Loading is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.
Key Takeaways
- The largest React performance win for most apps isn't memoization - it's shipping less JavaScript.
- Not analyzing your bundle first - use `rollup-plugin-visualizer` or webpack-bundle-analyzer to find what's actually large before deciding what to split
- Splitting too aggressively - splitting a 5KB component costs more in HTTP requests and waterfall delays than it saves; split chunks that are >50KB and conditionally rendered
- Missing Suspense boundary - `React.lazy` without `<Suspense>` throws an error; every lazy component needs at least one Suspense ancestor
Finished this lesson?