27

In the react documentation I found this way to import PureRenderMixin

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

How can it be rewritten 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.

Boris Zagoruiko
  • 12,705
  • 15
  • 47
  • 79

2 Answers2

33

Unfortunately import statements does not work like object destructuring. Curly braces here mean that you want to import token with this name but not property of default export. Look at this pairs of import/export:

 //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'

For your case you can import whole object and make a destructuring assignment

 import addons from "react/addons";
 let {addons: {PureRenderMixin}} = addons;
just-boris
  • 9,468
  • 5
  • 48
  • 84
  • 6
    `import {addons} from 'react/addons'; const {PureRenderMixin} = addons;` works too. Also take a look at https://github.com/gaearon/react-pure-render#mixin – Ilya Boyandin Jul 17 '15 at 08:28
4
import PureRenderMixin from 'react-addons-pure-render-mixin';

See example here.

Daniel Storch
  • 979
  • 3
  • 10
  • 25