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?
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With