Why es6 react component works only with “export default”?

前端 未结 2 1380
忘掉有多难
忘掉有多难 2020-11-28 00:11

This component does work:

export class Template extends React.Component {
    render() {
        return (
            
component
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 00:58

    Exporting without default means it's a "named export". You can have multiple named exports in a single file. So if you do this,

    class Template {}
    class AnotherTemplate {}
    
    export { Template, AnotherTemplate }
    

    then you have to import these exports using their exact names. So to use these components in another file you'd have to do,

    import {Template, AnotherTemplate} from './components/templates'
    

    Alternatively if you export as the default export like this,

    export default class Template {}
    

    Then in another file you import the default export without using the {}, like this,

    import Template from './components/templates'
    

    There can only be one default export per file. In React it's a convention to export one component from a file, and to export it is as the default export.

    You're free to rename the default export as you import it,

    import TheTemplate from './components/templates'
    

    And you can import default and named exports at the same time,

    import Template,{AnotherTemplate} from './components/templates'
    

提交回复
热议问题