Named Exports Don't Work with React.lazy
You'll learn why React.lazy() only accepts default exports and how to handle components that use named exports.
The Default Export Requirement
React.lazy() has a strict requirement: the dynamic import() must resolve to a module with a default export. If you try to lazy load a component that's exported as a named export, React will throw an error.
Think of it like a vending machine that only accepts one specific type of coin. React.lazy() is designed to look for exactly one thing—the default export—and won't work if you hand it something else.
The Problem
// MyComponent.js
export const MyComponent = () => <div>Hello</div>; // Named export
// App.js
const LazyComponent = React.lazy(() => import('./MyComponent')); // ❌ Won't work!
This fails because import('./MyComponent') returns a module object like { MyComponent: ... }, but React.lazy() expects { default: ... }.
The Solution: Re-export with Default
You need to create an intermediate module that re-exports your named export as a default:
// MyComponent.js (unchanged)
export const MyComponent = () => <div>Hello</div>;
// MyComponent.lazy.js (new file)
export { MyComponent as default } from './MyComponent';
// App.js
const LazyComponent = React.lazy(() => import('./MyComponent.lazy')); // ✅ Works!
Alternatively, use an inline arrow function to manually extract the named export:
const LazyComponent = React.lazy(() =>
import('./MyComponent').then(module => ({ default: module.MyComponent }))
);
Key Takeaway: React.lazy() only works with default exports—use a re-export wrapper file or transform the promise to handle named exports.