React - How to export a pure stateless component

前端 未结 4 1266
臣服心动
臣服心动 2021-01-31 00:58

How can I export a stateless pure dumb component?

If I use class this works:

import React, { Component } from \'react\';

export default class Header ext         


        
相关标签:
4条回答
  • 2021-01-31 01:37

    You can also use a function declaration instead of assignment:

    export default function Header() {
        return <pre>Header</pre>
    }
    

    In your example, you already use curly brackets and return so this is apparently matching with your needs with no compromise.

    0 讨论(0)
  • 2021-01-31 01:39

    Just as a side note. You could technically export default without declaring a variable first.

    export default () => (
      <pre>Header</pre>
    )
    
    0 讨论(0)
  • 2021-01-31 01:44

    you can do it in two ways

    const ComponentA = props => {
      return <div>{props.header}</div>;
    };
    
    export default ComponentA;

    2)

    export const ComponentA = props => {
      return <div>{props.header}</div>;
    };

    if we use default we import like this

    import ComponentA from '../shared/componentA'
    

    if we don't use default we import like this

    import { ComponentA } from '../shared/componentA'
    
    0 讨论(0)
  • 2021-01-31 01:49

    ES6 doesn't allow export default const. You must declare the constant first then export it:

    const Header = () => {
      return <pre>Header</pre>
    };
    export default Header;
    

    This constraint exists to avoid writting export default a, b, c; that is forbidden: only one variable can be exported as default

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