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

后端 未结 6 1627
隐瞒了意图╮
隐瞒了意图╮ 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:45

    To export a single component in ES6, you can use export default as follows:

    class MyClass extends Component {
     ...
    }
    
    export default MyClass;
    

    And now you use the following syntax to import that module:

    import MyClass from './MyClass.react'
    

    If you are looking to export multiple components from a single file the declaration would look something like this:

    export class MyClass1 extends Component {
     ...
    }
    
    export class MyClass2 extends Component {
     ...
    }
    

    And now you can use the following syntax to import those files:

    import {MyClass1, MyClass2} from './MyClass.react'
    

提交回复
热议问题