How to import and export components using React + ES6 + webpack?

后端 未结 6 1631
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 19:23

I\'m playing around with React and ES6 using babel and webpack. I want to build several components in different files, im

6条回答
  •  忘掉有多难
    2020-11-28 19:48

    There are two different ways of importing components in react and the recommended way is component way

    1. Library way(not recommended)
    2. Component way(recommended)

    PFB detail explanation

    Library way of importing

    import { Button } from 'react-bootstrap';
    import { FlatButton } from 'material-ui';
    

    This is nice and handy but it does not only bundles Button and FlatButton (and their dependencies) but the whole libraries.

    Component way of importing

    One way to alleviate it is to try to only import or require what is needed, lets say the component way. Using the same example:

    import Button from 'react-bootstrap/lib/Button';
    import FlatButton from 'material-ui/lib/flat-button';
    

    This will only bundle Button, FlatButton and their respective dependencies. But not the whole library. So I would try to get rid of all your library imports and use the component way instead.

    If you are not using lot of components then it should reduce considerably the size of your bundled file.

提交回复
热议问题