React.lazy React.lazy Code Splitting

Last updated: 2026-11-02

React.lazy React.lazy Code Splitting

React.lazy(() => import('./Page')) turns a component into a chunk that is only downloaded when it is actually needed, and Suspense shows placeholder UI while the chunk is in flight. The first paint then ships only the code of the home page and every other route is fetched on demand, which is the first big win for the speed of a single-page app and the easiest one to measure.

GroupRouting & State Mgmt
KindAPI
SinceReact 16.6

ℹ️ Info The editor transpiles JSX to JavaScript and renders the real UI in a sandbox; components mount automatically — no manual render call needed.

📝 Syntax

import { lazy, Suspense } from 'react'

const AdminPage = lazy(() => import('./AdminPage'))  // dynamic import splits the bundle

<Suspense fallback={<p>Loading…</p>}>
  <AdminPage />   {/* the chunk is requested at render time and replaces the fallback once ready */}
</Suspense>

⚙️ Parameters

factoryThe () => import('./X') factory function: it returns the module promise, and the module needs a default export (since React 18 you can use then to name a named export explicitly).

Returns:lazy(factory) returns a new component: the module starts loading on the first render and, until it resolves, throws up to the nearest Suspense boundary so it can show its fallback.

▶ Example

The example runs out of the box: it auto-renders when expanded, and edits take effect instantly.

Output:

Clicking the button shows a "downloading" placeholder for 1.2 seconds and then switches to the loaded content - the fallback to lazy component transition of Suspense (simulated demo).

💡 Key Points

⚠️ Warning The module of a lazy component must have a default export (or be wrapped in then), otherwise rendering throws; pair it with an ErrorBoundary to catch load failures.
💡 Tip Lazy loading is most often paired with routing: wrap every page Route in a lazy + Suspense pair and the first-paint JS often shrinks by half.
ℹ️ Info In local development the chunk loads instantly and you see no difference; build with vite build && vite preview to feel the real splitting latency.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q What is the relationship between lazy and Suspense?
A lazy declares "this component is loaded on demand", Suspense catches the "not ready yet" state and renders the fallback. The two always come as a pair.
Q The fallback flashes for a moment and it looks ugly
A Turn the fallback into a skeleton screen so the transition looks less jarring; you can also add modulepreload hints for critical chunks so the browser starts the download earlier.
Q My lazy component only has a named export
A Wrap it yourself: lazy(() => import('./X').then(m => ({ default: m.X }))) turns the named export into a default one.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏