Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use curly braces for ES6 import?

It seems to be obvious, but I found myself a bit confused about when to use curly braces for importing a single module in ES6. For example, in the React-Native project I am working on, I have the following file and its content:

File initialState.js

var initialState = {     todo: {         todos: [             {id: 1, task: 'Finish Coding', completed: false},             {id: 2, task: 'Do Laundry', completed: false},             {id: 2, task: 'Shopping Groceries', completed: false},         ]     } };  export default initialState; 

In the TodoReducer.js, I have to import it without curly braces:

import initialState from './todoInitialState'; 

If I enclose the initialState in curly braces, I get the following error for the following line of code:

Cannot read property todo of undefined

File TodoReducer.js:

export default function todos(state = initialState.todo, action) {     // ... } 

Similar errors also happen to my components with the curly braces. I was wondering when I should use curly braces for a single import, because obviously, when importing multiple component/modules, you have to enclose them in curly braces, which I know.

The Stack Overflow post at here does not answer my question, instead I am asking when I should or should not use curly braces for importing a single module, or I should never use curly braces for importing a single module in ES6 (this is apparently not the case, as I have seen single import with curly braces required).

like image 889
TonyGW Avatar asked Apr 22 '16 13:04

TonyGW


People also ask

When to use {} while importing in JS?

{} is used when you want to import part of an object. The * as searchView one will import all properties and methods in the searchView file.

Do you need curly brackets in JavaScript?

Curly braces { } are special syntax in JSX. It is used to evaluate a JavaScript expression during compilation. A JavaScript expression can be a variable, function, an object, or any code that resolves into a value.

What is the use of curly brackets in JavaScript?

Curly brackets { } is a code in javascript that groups altogether the code blocks or statements.. Its main purpose is to execute several code blocks or statements altogether..

What is curly braces in node JS?

In javascript code, curly brackets are used to deconstruct an object.


2 Answers

This is a default import:

// B.js import A from './A' 

It only works if A has the default export:

// A.js export default 42 

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

// B.js import 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.js import { 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.js export const A = 42 export const myA = 43 export 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.js import A, { myA, Something } from './A' 

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

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

We can also assign them all different names when importing:

// B.js import 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.

like image 160
Dan Abramov Avatar answered Oct 19 '22 07:10

Dan Abramov


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.

like image 44
prosti Avatar answered Oct 19 '22 07:10

prosti