Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "import {Something} from somelib" and "import Something from somelib" React.js [duplicate]

Tags:

import

reactjs

I do not understand the difference between:

import {Something} from somelib

and

import Something from somelib

in React.js.

Could someone, please, explain it?

like image 838
Raluca Avatar asked Jan 31 '18 07:01

Raluca


People also ask

What is the difference between import and export in react JS?

Introduction. Importing and exporting in React JS will help us write modular code, i.e., splitting code into multiple files. Importing allows using contents from another file, whereas exporting makes the file contents eligible for importing.

What is difference between import and export?

Exports describe selling products and solutions created in the home country to other markets. Imports are stemmed from the theoretical meaning of bringing in goods and services into the port of a country. An import in the obtaining country is an export to the sending nation.

Do you need to import React into every component?

You no longer need to import React from "react" . Starting from the release 17 of React, JSX is automatically transformed without using React. createElement . However, other exports like hooks must be imported.


1 Answers

When working with ES6 modules, you have two types of exports:

export Something
export default Something

The difference between these is how you import them. If you have a single file with multiple modules, it makes sense to give each module a name and be able to import each of them individually, without having to import the entire contents of the file.

For example, say you have 3 modules in one file, and you export them as export A; export B; export C;. You can then import any one of them using the curly brackets import syntax. So import {A, B}, for example will import module A and B only.

The default export syntax is commonly used in React when you want to export a component, and nothing else from a file. By exporting something with export default A, you can import that module by using the import X from ../file, where X is an alias and can be anything (but usually the same name is used for consistency).

You can read more about ES6 import & export here and here.

like image 198
bamtheboozle Avatar answered Sep 21 '22 21:09

bamtheboozle