Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 16.8 — Code Splitting with lazy
React · Example 1 of 2

lazy + Suspense: download on first use

The loader runs on the first render of Chart, not when the page loads. Once the module has arrived, lazy keeps it, so showing the chart again is instant.

Code splitting cuts your JavaScript into smaller files (chunks) that load only when needed: lazy(() => import("./Chart")) makes a component load on first render, <Suspense> shows a fallback meanwhile, and splitting by route plus preloading on hover keeps the first page fast without making later clicks slow.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Turn import MarkdownEditor from "./MarkdownEditor" into a lazily loaded component with a fallback.

02

The module ./Map.jsx exports export function MapView() {...} (no default). Load it lazily.

Explain it without notes

01

What is code splitting and why does it improve load time?

02

How do lazy and Suspense work together?

03

Why is route-level splitting a good default?

04

How can you avoid a loading delay when the user opens a lazily loaded feature?

lazy + Suspense: download on first use

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

Page loaded: nothing downloaded yet
Downloading the Chart chunk...
Area: Loading chart...
Chunk arrived
Area: 📈 Sales chart
Second time, no download: 📈 Sales chart