javascript
32 lines · 6 steps
Lazy-loaded routes in React
Split each page into its own bundle and load it on demand, with a spinner and an error net around the swap.
Explained by
highlit
1import { lazy, Suspense } from "react";
2import { Routes, Route, NavLink } from "react-router-dom";
3import ErrorBoundary from "./components/ErrorBoundary";
4import Spinner from "./components/Spinner";
5
6const Dashboard = lazy(() => import("./pages/Dashboard"));
7const Reports = lazy(() => import("./pages/Reports"));
8const Settings = lazy(() => import("./pages/Settings"));
9
10export default function AppRoutes() {
11 return (
12 <div className="app-shell">
13 <nav className="sidebar">
14 <NavLink to="/" end>Dashboard</NavLink>
15 <NavLink to="/reports">Reports</NavLink>
16 <NavLink to="/settings">Settings</NavLink>
17 </nav>
18
19 <main className="content">
20 <ErrorBoundary fallback={<p>Failed to load this section.</p>}>
21 <Suspense fallback={<Spinner label="Loading…" />}>
22 <Routes>
23 <Route path="/" element={<Dashboard />} />
24 <Route path="/reports" element={<Reports />} />
25 <Route path="/settings" element={<Settings />} />
26 </Routes>
27 </Suspense>
28 </ErrorBoundary>
29 </main>
30 </div>
31 );
32}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Wrapping dynamic imports in lazy() lets each route ship as a separate chunk fetched only when visited.
- 2Suspense supplies a loading fallback for any lazy child while its bundle downloads.
- 3An ErrorBoundary outside Suspense catches chunk-load failures so a bad network doesn't blank the whole app.
Related explainers
javascript
function evaluate(expression) { const tokens = tokenize(expression); let pos = 0;
Building a recursive descent calculator
parsing
recursion
operator-precedence
Intermediate
8 steps
javascript
import { useState, useEffect, useCallback, useRef } from 'react'; const cache = new Map(); const inflight = new Map();
Building a stale-while-revalidate hook in React
caching
request-deduplication
custom-hooks
Advanced
10 steps
javascript
import { useEffect, useRef, useState } from 'react'; export function useDelayedFlag(active, delay = 300) { const [visible, setVisible] = useState(false);
Delaying a loading spinner with a React hook
custom-hooks
debouncing
cleanup
Intermediate
8 steps
javascript
const SWIPE_THRESHOLD = 80; const MAX_TRANSLATE = 120; export function attachSwipeToDismiss(element, onDismiss) {
Building a swipe-to-dismiss gesture in JS
touch-events
gesture-detection
dom-manipulation
Intermediate
10 steps
javascript
const { pool } = require('./db'); function withTransaction() { return async (req, res, next) => {
A per-request transaction middleware in Express
middleware
database-transactions
connection-pooling
Advanced
7 steps
javascript
function collapseConsecutiveLogs(lines, { keyFn = (l) => l.message } = {}) { const groups = []; for (const line of lines) {
Collapsing consecutive log lines in JavaScript
grouping
run-length-encoding
data-transformation
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/lazy-loaded-routes-in-react-explained-javascript-6d95/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.