I can see there are two different ways to import
import React from 'react'
import { render } from 'react-dom'
the second one has brackets. So what is the difference between the two? and when should I add brackets? thanks
I can see there are two different ways to import
import React from 'react'
import { render } from 'react-dom'
the second one has brackets. So what is the difference between the two? and when should I add brackets? thanks
Consider
primitives.js
,it can be imported like this,
In this case,
sum
is called a "Default Export" and a module may contain a single "Default Export" only.sub
andsqr
are called "Named Export" and a module may contain multiple named exports.Curly Braces are used to import
single(specific) property
, whereas the word without braces isimport
entire object
form that file.Eg.,
Here the word
React
represents to importentire object
from the file'react'
{Component}
means we specify to import theparticular property
from the file.No need to add bracket if you exporting as default. you can have only default in the module.
case1:
case2:
case3:
export default sum;
you can import the default
Well the difference between whether you should import your components
within brackets or without it
lies in the way youexport
it.There are two types of exports
A component can have
one default export and zero or more named
exportsIf a component is a default export then you need to import it without brackets E.g.
The import it as
A named export could be like
or
The you can import it as
or
If your component has one default export and few named export, you can even mix them together while importing
Similarly in case of
react
andreact-dom
,React
andReactDOM
aredefault exports
respectively whereas, for instanceComponent
is anamed export
inreact
andrender
is a named export inreact-dom
. Thats the reason you can either doand then use
or use it like mentioned in your question.