Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to do multiple class imports with ES6/Babel?

I'm working on a react project (my first) and I've recently restructured my folder structure to make a bit more sense.

To make my life easier, within my component folders, I have an index.js file which looks like the following:

export * from './App'; export * from './Home'; export * from './PageWrapper'; 

(The idea was lifted from another StackOverflow Question)

In this case each of the files this index points to have a singular class export.

Now in my main application, I try and do something like:

import {Home, App} from './containers/index'; //or import Home from './containers/index'; 

Nothing works. I've found that if I separate them all out into individual lines pointing directly at the correct file, it works.

import Home from './containers/Home'; import App from './containers/App'; 

So is it possible to import multiple classes the way I'm doing it, and I'm just not seeing it? Do I perhaps need to name them all (App as App)? Or is this simply an enforced limitation?

like image 692
cdutson Avatar asked Jan 07 '16 05:01

cdutson


People also ask

Can I use ES6 imports?

Importing can be done in various ways:Node js doesn't support ES6 import directly. If we try to use import for importing modules directly in node js it will throw out the error.

How do I export multiple classes?

Use named exports to export multiple classes in JavaScript, e.g. export class A {} and export class B {} . The exported classes can be imported by using a named import as import {A, B} from './another-file. js' . You can have as many named exports as necessary in a file.

Does ES6 import export?

With the help of ES6, we can create modules in JavaScript. In a module, there can be classes, functions, variables, and objects as well. To make all these available in another file, we can use export and import. The export and import are the keywords used for exporting and importing one or more members in a module.

How do I export multiple Const?

Use named exports to export multiple variables in TypeScript, e.g. export const A = 'a' and export const B = 'b' . The exported variables 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.


2 Answers

You can export like this:

import App from './App'; import Home from './Home'; import PageWrapper from './PageWrapper';  export {     App,     Home,     PageWrapper } 

Then, you can import like this wherever you need it:

import { App, PageWrapper } from './index' //or similar filename  ... 

You can read more about import and export here. I also answered a similar question here.

like image 88
Mario Tacke Avatar answered Oct 01 '22 22:10

Mario Tacke


I use export that looks something like this. In react it worked well

export {default as PublicRoute} from './PublicRoute'; export {default as PrivateRoute} from './PrivateRoute'; 

Then, you can import like this wherever you need:

import {PublicRoute, PrivateRoute} from './config/router'; ... 
like image 28
Matt Nguyen Avatar answered Oct 01 '22 22:10

Matt Nguyen