How to import part of an object into ES6 modules

In edit documentation I found a way to import PureRenderMixin

var PureRenderMixin = require('react/addons').addons.PureRenderMixin; 

How can I rewrite it in ES6 style. The only thing I can do is:

 import addons from "react/addons"; let PureRenderMixin = addons.addons.PureRenderMixin; 

I hope there is a better way.

+7
javascript ecmascript-6 module commonjs browserify
source share
2 answers

Unfortunately , import instructions do not work like destructuring objects . Curly braces here means that you want to import a token with this name, but not the default export property. Look at these import / export pairs:

  //module.js export default 'A'; export var B = 'B'; //script.js import A from './a.js'; //import value on default export import {B} from './a.js'; // import value by its name console.log(A, B); // 'A', 'B' 

In your case, you can import the whole object and complete the destruction assignment

  import addons from "react/addons"; let {addons: {PureRenderMixin}} = addons; 
+14
source share
 import PureRenderMixin from 'react-addons-pure-render-mixin'; 

See an example here .

+3
source share

All Articles