3

I am currently working on a react meteor project. I didn't find any clear documentation when to exactly use export default and when export const. Any opinions on this respectively when to use what and what are the differences?

Chiru
  • 2,633
  • 1
  • 15
  • 28
dom
  • 449
  • 6
  • 13

1 Answers1

10

export default exports your module with no name, you can thus import it with this syntax :

export default MyModule = () => console.log('foo')

import MyModule from './MyModule' //it works
import foobar from './MyModule' //it also works,

export const exports with name :

export const MyModule = () => console.log('foo')

import MyModule from './MyModule' //returns empty object since there is no default export
import { MyModule } from './MyModule' //here it works because by exporting without 'default' keyword we explicitly exported MyModule
  • So, when you're exporting only one element from your module and you don't care of its name, use export default.
  • If you want to export some specific element from your module and you do care of their names, use export const
  • You should notice that you can combine both, in case you want to import a specific module by default and let the user import specific elements of your module.
Pierre Criulanscy
  • 7,771
  • 3
  • 20
  • 36