When should I use curly braces for ES6 import? When should I use curly braces for ES6 import? javascript javascript

When should I use curly braces for ES6 import?


This is a default import:

// B.jsimport A from './A'

It only works if A has the default export:

// A.jsexport default 42

In this case it doesn’t matter what name you assign to it when importing:

// B.jsimport A from './A'import MyA from './A'import Something from './A'

Because it will always resolve to whatever is the default export of A.


This is a named import called A:

import { A } from './A'

It only works if A contains a named export called A:

export const A = 42

In this case the name matters because you’re importing a specific thing by its export name:

// B.jsimport { A } from './A'import { myA } from './A' // Doesn't work!import { Something } from './A' // Doesn't work!

To make these work, you would add a corresponding named export to A:

// A.jsexport const A = 42export const myA = 43export const Something = 44

A module can only have one default export, but as many named exports as you'd like (zero, one, two, or many). You can import them all together:

// B.jsimport A, { myA, Something } from './A'

Here, we import the default export as A, and named exports called myA and Something, respectively.

// A.jsexport default 42export const myA = 43export const Something = 44

We can also assign them all different names when importing:

// B.jsimport X, { myA as myX, Something as XSomething } from './A'

The default exports tend to be used for whatever you normally expect to get from the module. The named exports tend to be used for utilities that might be handy, but aren’t always necessary. However it is up to you to choose how to export things: for example, a module might have no default export at all.

This is a great guide to ES modules, explaining the difference between default and named exports.


I would say there is also a starred notation for the import ES6 keyword worth mentioning.

enter image description here

If you try to console log Mix:

import * as Mix from "./A";console.log(Mix);

You will get:

enter image description here

When should I use curly braces for ES6 import?

The brackets are golden when you need only specific components from the module, which makes smaller footprints for bundlers like webpack.


Dan Abramov's answer explains about the default exports and named exports.

Which to use?

Quoting David Herman: ECMAScript 6 favors the single/default export style, and gives the sweetest syntax to importing the default. Importing named exports can and even should be slightly less concise.

However, in TypeScript named export is favored because of refactoring. Example, if you default export a class and rename it, the class name will change only in that file and not in the other references, with named exports class name will be renamed in all the references.Named exports is also preferred for utilities.

Overall use whatever you prefer.

Additional

Default export is actually a named export with name default, so default export can be imported as:

import {default as Sample} from '../Sample.js';