Import Default Export
There can only be one default
export.
A JavaScript module is allowed any number of named exports. But default exports are in short supply. You get one and only one.
This limit on defaults empowers a sugary syntax on the import side.
We can assign the default export to any variable name, without fuss.
So, what should be the default?
In my last post, I demonstrated how I use named exports to expose UI components that are designed to work together.
With this structure, default
becomes a natural place to expose a, default composition of those components for drop-in use.
Now, anywhere I want the default composition, I maximally renamable import.
A note on exporting functions
Because default exports aren’t referenced by the provided name, there’s a tendency to leave off of functions names.
“It’s cleaner!”
Unfortunately, leaving the name off will impact your developer experience. DevTools will treat these references as anonymous functions.
So, I recommend adding keeping a descriptive name.
My take
I love default exports — when used as intended.
A lot of folks in frontend recommend using only default exports and mandating one-component-per-file. This feels contrary to the design of JavaScript modules. And the practice has the secondary effect of requiring bespoke file structures to communicate components relationships and private components — something modules already do well.
Use named exports and defaults — together — as intended.
Go pro
This is part of a course I’m build on modules at lunch.dev.
When live, members get access to this and other courses on React.