Named Exports with React.lazy()
You'll learn how to handle named exports when code-splitting with React.lazy(), which only accepts default exports.
The Default Export Constraint
React.lazy() has a strict requirement: it only works with default exports. If your component uses a named export (like export const MyComponent = ...), React.lazy() will fail.
This is because React.lazy() expects a Promise that resolves to a module with a .default property. Named exports don't provide this structure automatically.
The Wrapper Solution
When you need to lazy-load a component with a named export, you have two options:
Option 1: Create an intermediate module
If MyComponent.jsx exports:
export const MyComponent = () => <div>Hello</div>;
Create a wrapper file MyComponent.default.jsx:
export { MyComponent as default } from './MyComponent';
Then use:
const MyComponent = React.lazy(() => import('./MyComponent.default'));
Option 2: Inline re-export
Transform the named export during import:
const MyComponent = React.lazy(() =>
import('./MyComponent').then(module => ({
default: module.MyComponent
}))
);
This pattern unwraps the named export and rewraps it as a default export, satisfying React.lazy()'s requirement.
Why This Matters
Think of React.lazy() as a vending machine that only accepts dollar bills (default exports). If you have quarters (named exports), you need to exchange them first before the machine will work.
Key Takeaway: React.lazy() requires default exports—use .then(module => ({ default: module.NamedExport })) to adapt named exports on-the-fly.