Over a million developers have joined DZone.

ES6 Nested Imports (Babel + React)

Here's a quick tip on using the proper module imports in JavaScript ES6 for Babel and ReactJS

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

With the ES 6 module system, you have a choice of whether to use a single default export:

 export default DefaultObject 

... or potentially many named exports:

 export const NondefaultObject = {} 

You import these slightly differently, but otherwise, they work the same:

import DefaultObject from './DefaultObject'
import {NondefaultObject} from './NondefaultObject'

const App = () => (
    <NondefaultObject />

Where things go awry is when you want to aggregate imports, as per Jack Hsu’s excellent article on Redux application structure.

import * as do from './DefaultObject'
import * as ndo from './NondefaultObject'

const App = () => (
    <do.DefaultObject/>       // Does NOT work
    <ndo.NondefaultObject />  // works
    <do.default>              // works

Why is it so? When you import a default export, the name of the object is actually “default”. Somewhere in the Babel/Redux/React magic factory, somebody is clever enough to use the module name as an alias for its own default export when you use that module name in a JSX tag. However, when you assign that same default export to another value and then try to use that value (as in the import * case), no such magic occurs.

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

es-6,javascript,front-end,web development,react js

Published at DZone with permission of Jaime Metcher, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}