Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 exporting/importing in index file

I am currently using ES6 in an React app via webpack/babel. I am using index files to gather all components of a module and export them. Unfortunately, that looks like this:

import Comp1_ from './Comp1.jsx'; import Comp2_ from './Comp2.jsx'; import Comp3_ from './Comp3.jsx';  export const Comp1 = Comp1_; export const Comp2 = Comp2_; export const Comp3 = Comp3_; 

So I can nicely import it from other places like this:

import { Comp1, Comp2, Comp3 } from './components'; 

Obviously that isn't a very nice solution, so I was wondering, if there was any other way. I don't seem to able to export the imported component directly.

like image 399
MoeSattler Avatar asked Dec 03 '15 17:12

MoeSattler


People also ask

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.

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.


1 Answers

You can easily re-export the default import:

export {default as Comp1} from './Comp1.jsx'; export {default as Comp2} from './Comp2.jsx'; export {default as Comp3} from './Comp3.jsx'; 

There also is a proposal for ES7 ES8 that will let you write export Comp1 from '…';.

like image 103
Bergi Avatar answered Sep 28 '22 20:09

Bergi