Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-export a default export

What is the best way to re-export a default export from another file?

I'd like to do something like this:

export Button from './Button/Button';

Apparently this is not valid EcmaScript syntax (however it used to work in older Babel versions).

As a result I want to import {Button} from 'components';

Is there a better way, maybe a one-liner? ES6 syntax would be preffered.

Thanks!

like image 460
amann Avatar asked Jan 03 '16 11:01

amann


People also ask

How do I reexport my default import?

By creating an index. js file in the /utils folder and import all the defaults of all the utilities there and re-export, so the index file will serve as the "gateway" for all imports related to that folder. export { default as MyModule } from "./my-modue.

Can you export export default?

Exports without a default tag are Named exports. Exports with the default tag are Default exports. Using one over the other can have effects on your code readability, file structure, and component organization. Named and Default exports are not React-centric ideas.

What is the difference between export and re-export?

In simple terms, exports mean export of domestic goods moved out to a foreign country. Re-exports means export of foreign goods which already imported to the country from a foreign country.

Can you export default twice?

We had to wrap the named import in curly braces. You can have only one default export per file, but you can have as many named exports as necessary. If you don't want to use a named export, move the second variable to a separate file and make sure to stick to a maximum of 1 default export per file.


Video Answer


1 Answers

You can use the export { x as y } syntax:

export { default as Button } from './Button/Button';

I'd like to do something like this:

export Button from './Button/Button';

There is a stage 1 proposal for this syntax: https://github.com/leebyron/ecmascript-more-export-from

like image 131
Nicolò Avatar answered Sep 30 '22 06:09

Nicolò