DEV Community

Cover image for 🦥 React.lazy without a default export
All In Developer
All In Developer

Posted on Edited on Originally published at all1n.dev

🦥 React.lazy without a default export

React.lazy makes code splitting a one-liner, as long as the component you load is a default export. Most codebases I work in use named exports, so I needed a way to lazy load them without rewriting every module.


👉 Continue reading on my blog: 🦥 React.lazy without a default export

Top comments (5)

Collapse
 
balines_f053b1282 profile image
Carlos Alvidrez •

Thank you!

Collapse
 
gollyjer profile image
Jeremy Gollehon •

Does this have an affect on tree-shaking?

Collapse
 
all1ndev profile image
All In Developer •

Yes. Dynamic import cannot be analyzed at build time.
You will have to follow React Docs and reexport only wanted component to have dynamic import with tree shaking
reactjs.org/docs/code-splitting.ht...

Collapse
 
gollyjer profile image
Jeremy Gollehon •

Excellent. Thanks for the link.

Collapse
 
chenxiaochun profile image
chenxiaochun •

Thank you!