Adding Lazy Loading
Lazy loading in React means splitting your JavaScript bundle so that the code for some components downloads only when it is actually needed, usually when the user visits a particular route. Instead of sending the whole app on the first visit, the browser loads a smaller main file, then fetches extra pieces later. It is a common step just before building an app for production, because it shortens the time to the first usable screen.
How code splitting works
A normal import at the top of a file is static: the bundler puts that module in the same output file as the code that imports it. A dynamic import('./Page') call returns a promise and tells bundlers such as Vite or webpack to create a separate file, often called a chunk. React's lazy() function turns that promise into a component you can render, and Suspense shows a fallback while the chunk is downloading.
Lazy loading routes
Routes are the natural place to split, since a visitor reading the home page has no use for the admin dashboard code.
import { lazy, Suspense } from 'react';
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import Home from './pages/Home'; // loaded up front
const Dashboard = lazy(() => import('./pages/Dashboard'));
const Reports = lazy(() => import('./pages/Reports'));
export default function App() {
return (
<BrowserRouter>
<Suspense fallback={<p className="loading">Loading page...</p>}>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/dashboard" element={<Dashboard />} />
<Route path="/reports" element={<Reports />} />
</Routes>
</Suspense>
</BrowserRouter>
);
}
When someone opens /dashboard, React starts downloading that chunk, shows the fallback, and swaps in the page once it arrives. Later visits to the same route reuse the already loaded code.
Checking that it worked
After running the production build, look at the output folder. You should see several JavaScript files instead of one, with names hinting at the split pages. In the browser, open the Network tab, load the home page, then navigate to the dashboard: a new script request should appear only at that moment. If everything still lands in one file, the page is probably imported statically somewhere else in the code.
What to split and what to keep
- Good candidates: rarely visited pages, admin areas, heavy charts, rich text editors, maps and large modal contents.
- Keep in the main bundle: the landing page, layout, navigation and anything visible on first paint. Lazy loading these only adds a loading flash.
- Grouping: splitting into dozens of tiny chunks adds request overhead; split along natural boundaries such as routes.
Pitfalls that break lazy loading
lazy() expects the module to have a default export. If Dashboard.jsx uses export function Dashboard, the lazy component fails. Either add a default export or map it: lazy(() => import('./Dashboard').then((m) => ({ default: m.Dashboard }))).
Call lazy() at the top level of a module, never inside a component. Declaring it inside a render creates a new component each time, which resets state and reloads the chunk.
Network failures are the other real risk. If a user keeps a tab open while you deploy a new version, the old chunk file names may no longer exist on the server, and the dynamic import rejects. Wrap lazy routes in an error boundary that shows a friendly message and a reload button, so users are not left with a blank screen.
A good default
Split by route with lazy() and dynamic import(), give Suspense a light fallback, keep first-screen components in the main bundle, use default exports for lazy modules, and add an error boundary for failed chunk loads. Then confirm the split with the build output and the Network tab.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.