How to import part of object in ES6 modules

前端 未结 2 1185
醉话见心
醉话见心 2020-11-30 10:10

In the react documentation I found this way to import PureRenderMixin

var PureRenderMixin = require(\'react/addons\').addons.PureRenderMixin;
相关标签:
2条回答
  • 2020-11-30 10:25

    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;
    
    0 讨论(0)
  • 2020-11-30 10:25
    import PureRenderMixin from 'react-addons-pure-render-mixin';
    

    See example here.

    0 讨论(0)
提交回复
热议问题