import{ lazy,Suspense,useState}from'react';constAdminPanel=lazy(()=>import('./AdminPanel'));constUserDashboard=lazy(()=>import('./UserDashboard'));functionApp({ role }){return(<Suspense fallback={<div className="spinner">Loading...</div>}>{role ==='admin'?<AdminPanel/>:<UserDashboard/>}</Suspense>);}
Note React.lazy only works with default exports. For named exports, create an intermediate module that re-exports as default. Suspense can also wrap components that use the use() hook to read promises in React 19.
Note Route-based code splitting gives the biggest bundle size reduction because entire page components and their dependencies are loaded on demand. Place the Suspense boundary around the lazy components, not the entire app.
Note Returns a promise that resolves to the module namespace. Ideal for code-splitting, lazy loading, and conditional imports. Works at runtime, not just at the top of a file.
Frequently asked questions
How does React handle code splitting?
This task is covered in 2 stacks on this page: React, JavaScript. The "Suspense & Lazy Loading" snippet in React uses `const LazyComponent = React.lazy(() => import('./Component'));`.
Which code does the React example use?
The "Suspense & Lazy Loading" snippet uses `const LazyComponent = React.lazy(() => import('./Component'));`, from the Patterns section of the React cheat sheet.
Which stacks cover "code splitting" on this page?
React, JavaScript. Together they hold 3 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Suspense & Lazy Loading": React.lazy only works with default exports. For named exports, create an intermediate module that re-exports as default. Suspense can also wrap components that use the use() hook to read promises in React 19.