DEV Community

Discussion on: Picking apart JavaScript import Syntax

Collapse
 
irreverentmike profile image
Mike Bifulco • Edited

That's a perfect example, and a great explanation. I think a more thorough explanation is probably due in my post.

Curiosity got the best of me, and I set up a minimal example of what I mentioned in the post... and it turns out it works!

🤔 To be honest, the more I think about it, the more confused I am. If you dig into react's source, you can see useState is exported just like your example above, with useState a child of the object that is exported by default. I wonder if there's not also a separate export for useState, too?

Collapse
 
vramana profile image
Ramana Venkata • Edited

Ah! I did not look at React's source. So my argument is wrong as well.

My general point is that you should not use non-default exports from the default export. This may mislead your readers.

import * from React from 'react' would be correct way to do it. But react does not individual exports so it does not apply.

One reason why React may be using default export object is because babel transpiles JSX to React.createElement('div', ...) calls. If the default export is not an object, then we would have write

import { createElement } from 'react'

React team may have felt it's better to write import React from 'react' instead.