Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is proper way to import multiple components from same file in react instead of getting each component imported

I have multiple components or multiple constants in one file, how do i import all that components or constants. Is there any way to import all the components instead of mentioning each component in import statement. please refer below exampe;

`
//Constants.js
export const var1="something"
export const var2="something"
export const var3="something"
export const var4="something"
export const var5="something"
export const var6="something"
export const var7="something"
.
.
.


//App.js

import {var1,var2,var3,var4,......} from './Constants'
`

instead of this is there any way?

like image 726
Suraj Bande Avatar asked Mar 28 '18 06:03

Suraj Bande


People also ask

How do you import and export multiple components in React?

Use named exports to export multiple components in React, e.g. export function A() {} and export function B() {} . The exported components can be imported by using a named import as import {A, B} from './another-file' . You can have as many named exports as necessary in a single file.

How do you import the same name components in React?

To use import aliases when importing components in React, use the as keyword to rename the imported component, e.g. import {Button as MyButton} from './another-file' . The as keyword allows us to change the identifying name of the import.


1 Answers

The other way is using the * syntax:

import * as Constants from './Constants';
...
// use as so:
const x = 5 * Constants.var1;

Read more: http://2ality.com/2014/09/es6-modules-final.html

like image 163
kumarharsh Avatar answered Sep 25 '22 16:09

kumarharsh