49

I can see there are two different ways to import:

import React from 'react'
import { render } from 'react-dom'

The second one has brackets. What is the difference between the two? And when should I add brackets?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Benjamin Li
  • 1,687
  • 7
  • 19
  • 30
  • Suggested reading http://www.2ality.com/2014/09/es6-modules-final.html no need to ask this on SO – Andy Ray Dec 27 '16 at 00:49
  • Brief answer: this is es6 destructuring coming into play. `var React` will be assigned to everything exported from `'react'`, and `var render` will be assigned to the `render` property of whatever is exported from `'react-dom'` – therobinkim Dec 27 '16 at 00:50
  • 2
    Possible duplicate of [When should I use curly braces for ES6 import?](http://stackoverflow.com/questions/36795819/when-should-i-use-curly-braces-for-es6-import) – jpdelatorre Dec 27 '16 at 02:14

5 Answers5

79

Well, the difference between whether you should import your components within brackets or without it lies in the way you export it.

There are two types of exports

  1. Default Export
  2. Named Export

A component can have one default export and zero or more named exports.

If a component is a default export then you need to import it without brackets.

E.g.,

export default App;

The import it as

import App from './path/to/App';

A named export could be like

export const A = 25;

or

export {MyComponent};

Then you can import it as

import {A} from './path/to/A';

or

import {A as SomeName} from './path/to/A';

If your component has one default export and few named exports, you can even mix them together while importing

import App, {A as SomeName} from './path/to/file';

Similarly in case of react and react-dom, React and ReactDOM are default exports respectively whereas, for instance Component is a named export in react and render is a named export in react-dom. That's the reason you can either do

import ReactDOM from 'react-dom';

and then use

ReactDOM.render()

or use it like mentioned in your question.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
  • this answer seems very definitive... **one** default export **plus** multiple named exports (allowed per script file)... and then to import default export: no braces, import named export: has to use braces. Is one of the official reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export ? (part of the ES6 standard) – nonopolarity Apr 05 '17 at 15:10
  • 3
    But why is it made this distinction between default and named :)? – Enrico Aug 16 '18 at 18:57
7

First of all, a big thank you to all the other answers.

Here is a summary of all the above, in one answer.

Context with examples

import React from 'react';          // Importing without braces
import { render } from 'react-dom'; // Importing with braces

To be able to understand import, it's important to firstly understand export and its types.

Types of exports

There are mainly two types of exports, 'default' and 'named'. Whether to use braces or not, depends entirely on which type of exported variable is being imported.

So, the short answer is that variables exported as default, do not need braces, but named variables do need to be imported with braces.

Now, let's look at some practical examples of how to import and export both types.

Default: How to export and import

  • exporting
// Module1.js
export default App;

// Module2.js
export default myVariable;

// Module3.js
export default myFunction;

// Please note that there can only be one default export per module!
  • importing
import App from './Module1'
import AppRenamed from './Module1'

import myVariable from, './Module2'
import myFunction from './Module3'

// Please note that default modules can be renamed when importing
// ... and they will still work!

Named: How to export and import

  • exporting
export const A = 42
export const myA = 43
export const Something = 44

export { cube, foo, graph };

// Note how there can be several named exports per module
// exported in groups or individually
  • importing
import { A, myA } from './my-module.js';
import { Something as aRenamedVar } from './my-module.js';
import { cube } from './my-module.js';
import { foo, graph } from './my-module.js';

// Likewise, named exports can be imported in groups or individually

Other notes

  • let's revisit the very first example we saw above
import React from 'react'
import { render } from 'react-dom'
  • please note that although, React doesn't use braces, and render does, render is actually a part of react-dom.
  • therefore it is also possible to import the entire default react-dom without braces and then use render
import React from 'react'
import ReactDOM from 'react-dom'

ReactDOM.render()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Grateful
  • 9,685
  • 10
  • 45
  • 77
3

Consider primitives.js,

export default (a, b) => a + b;
export const sub = (a, b) => a - b;
export const sqr = a => a**2;

It can be imported like this,

import sum, { sub, sqr } from './primitives';

In this case, sum is called a "Default Export" and a module may contain a single "Default Export" only.

sub and sqr are called "Named Export" and a module may contain multiple named exports.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shakil
  • 1,044
  • 11
  • 17
3

Curly braces are used to import single(specific) property, whereas the word without braces is import entire object form that file.

For example,

import React, { Component } from 'react';

Here the word React represents to import entire object from the file 'react'.

{Component} means we specify to import the particular property from the file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Arul
  • 1,420
  • 1
  • 15
  • 25
0

There isn't any need to add brackets if you are exporting as default. You can have only default in the module.

Case 1:

export default function(num1, num2) {
    return num1 + num2; }

Case 2:

function sum(num1, num2) {
    return num1 + num2; }

export { sum as default };

Case 3:

function sum(num1, num2) {
    return num1 + num2; }

export default sum;

You can import the default

import sum from "./test.js";

console.log(sum(1, 2));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Khalid Azam
  • 1,615
  • 19
  • 17