Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline import the default export into array

I have multiple files called PageX, PageY, PageZ etc.

Each of these have an export default SETUP { path:'blah_X', component:X }.

In my index.js I want to import these SETUPs into an array like this:

const PAGE_SETUPS = [
   import './PageX',
   import './PageY',
   import './PageZ' 
];

const routes = PAGE_SETUPS.map(setup => createElement(Route, {path:setup.path, component:setup.component});

I am hitting all kinds of issues. Is this possible to inline-import the default exported item into an array like this?

like image 760
Noitidart Avatar asked Mar 04 '17 11:03

Noitidart


People also ask

Can I export default an array?

When you want to export a single function, class, object, variable, array, or any other stuff, use default export. You can import the default exported component with any name.

What is default export in TypeScript?

Default exports are meant to act as a replacement for this behavior; however, the two are incompatible. TypeScript supports export = to model the traditional CommonJS and AMD workflow. The export = syntax specifies a single object that is exported from the module.

What is a default export?

Default export - is the value that will be imported from the module, if you use the simple import statement import X from 'module'. X is the name that will be given locally to the variable assigned to contain the value, and it doesn't have to be named like the origin export. There can be only one default export.

How does import and export work in JavaScript?

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.


2 Answers

No, there are no "inline imports" in ES6 modules. You can either use whichever kind of non-declarative import method the module loading system that you transpile to offers, or you have to spell it out:

import PageX from './PageX',
import PageY from './PageY',
import PageZ from './PageZ' 
const PAGE_SETUPS = [PageX, PageY, PageZ];
like image 140
Bergi Avatar answered Jan 03 '23 06:01

Bergi


What I would do is have this be in a pages directory with an index.mjs file alongside all your pages. Then you can import all your pages and export the array. From that index file you could export an array of your pages and import that in another module.

// pages/index.js
import PageX from './PageX',
import PageY from './PageY',
import PageZ from './PageZ' 
export const PAGE_SETUPS = [PageX, PageY, PageZ];

Then you would just need to import your array in another file.

//some other module
import PAGE_SETUPS from './pages/index.mjs'
const routes = PAGE_SETUPS

Alternatively you could pass your modules through the original page/index.js file instead of exporting an array.

// pages/index.js
export * from './PageX',
export * from './PageY',
export * from './PageZ' 
// some other module
import {PageX, PageY, PageZ} from './pages/index.mjs'

const routes = [
  PageX,
  PageY, 
  PageZ
]
like image 41
James Avatar answered Jan 03 '23 07:01

James