Full answer
A single JavaScript bundle forces every visitor to download code for pages and features they may never open. Code splitting breaks the bundle into chunks that load on demand.
const Chart = lazy(() => import('./Chart.jsx')) does this. The call to import() is a dynamic import: the bundler (Vite, webpack, Rollup) sees it and emits a separate chunk. The lazy component fetches that chunk the first time it is rendered. Until it arrives, React suspends and the nearest <Suspense fallback={...}> above shows the fallback.
Rules to remember. The module must have a default export that is a component. For a named export, adapt it: lazy(() => import('./charts').then((m) => ({ default: m.Chart }))). Call lazy at module level, not inside a component, otherwise a new lazy type is created on every render and its state resets. Wrap lazy components in Suspense, and in an error boundary, because a failed network request for the chunk throws an error.
The best places to split are routes (each page is a chunk), modals and panels that appear after a click, and large libraries used in one corner of the UI. Splitting a ten-line component costs a network request and gives no saving.
For smoother navigation, update state inside startTransition so React keeps the old screen visible instead of replacing it with the fallback. You can also preload a chunk early, for example on hover, by calling the same import() ahead of time.
Frameworks often split by route automatically, and Server Components keep server-only code out of the browser bundle entirely, so manual lazy is mostly for client-side apps and heavy client widgets.
Chart.jsx, in its own file, is a plain component:
export default function Chart() {
return <p>Chart loaded: 3 bars</p>;
}import { lazy, Suspense, useState } from 'react';
const Chart = lazy(() => import('./Chart.jsx'));
export default function Dashboard() {
const [showChart, setShowChart] = useState(false);
return (
<div>
<button onClick={() => setShowChart(true)}>Show chart</button>
{showChart && (
<Suspense fallback={<p>Loading chart...</p>}>
<Chart />
</Suspense>
)}
</div>
);
}This file expects the Chart.jsx module shown in the answer next to it. The chunk is requested only after the button is pressed. In this check the bundler placed Chart.jsx in the same output file, so the lazy load resolves at once, whereas a production build emits a separate chunk. Rendered result after the click: <div><button>Show chart</button><p>Chart loaded: 3 bars</p></div>
Key points
- lazy(() => import('./X')) loads a component's code on first render.
- Suspense shows a fallback while the chunk loads. Add an error boundary for failures.
- The imported module must have a default export, or be adapted with then.
- Call lazy at module level, never inside a component.
- Split routes, modals and heavy widgets, not small components.
Common mistakes
- Calling lazy inside a component, which recreates the lazy type on each render.
- Importing a named export without mapping it to default.
- Forgetting a Suspense boundary, which makes React throw an error.
- Splitting very small components and adding extra network requests.
- Not handling chunk load failures after a new deployment.
Follow-ups
- How would you avoid a loading spinner flash when navigating between lazy routes?
- How can you preload a lazy component before the user needs it?
- How do Server Components change the need for manual code splitting?
Tips
- Mention where you would split (routes, modals, heavy widgets). It shows judgement.
- State the default-export rule and the named-export workaround.
- Add the failure case: a chunk can fail to load, so use an error boundary.