Basic React.lazy() Syntax
You'll learn the exact syntax for lazily loading React components using dynamic imports.
The Dynamic Import Pattern
React.lazy() accepts a function that returns a dynamic import() call. This simple pattern tells React: "Don't load this component's code until I actually need to render it."
Here's the basic syntax:
const LazyComponent = React.lazy(() => import('./MyComponent'));
Breaking this down:
React.lazy()takes a function (not the import itself!)- That function must return
import('./path')— a Promise - The imported module must have a default export
Why the Function Wrapper?
Think of it like a recipe card vs. actually cooking. React.lazy(() => import(...)) hands React the recipe for loading the component. React decides when to follow that recipe (when the component is actually rendered). If you called import() directly, you'd start cooking immediately—defeating the lazy-loading purpose.
The Default Export Requirement
The lazy-loaded component must be exported as default:
// MyComponent.jsx - ✅ Correct
export default function MyComponent() {
return <div>Lazy loaded!</div>;
}
// ❌ Won't work with React.lazy()
export function MyComponent() { ... }
If you have named exports, create a re-export file or refactor to default exports.
Using Your Lazy Component
Once defined, wrap it in a Suspense boundary:
<Suspense fallback={<div>Loading...</div>}>
<LazyComponent />
</Suspense>
Without Suspense, React throws an error—lazy components require a boundary to show something while loading.
Key Takeaway: React.lazy() takes a function returning import('./Component'), and the component must be a default export. Always wrap usage in a Suspense boundary.