Is React.Component the default extension when exporting?

后端 未结 2 1613
陌清茗
陌清茗 2021-01-15 06:13

I am looking through some React projects, and sometimes see-

export default () => {

But other times I see-

export default class

2条回答
  •  伪装坚强ぢ
    2021-01-15 06:41

    The export default () => you see is a React 0.14+ "Functional Component".

    It's a new more concise syntax for writing React components. Both it and the other syntax are fine.

    These components behave just like a React class with only a render method defined. Since no component instance is created for a functional component, any ref added to one will evaluate to null. Functional components do not have lifecycle methods, but you can set .propTypes and .defaultProps as properties on the function.

    Basically doing:

    class MyComponent extends React.Component {
      render() {
        return 

    Hello

    ; } }

    Is the same as:

    const MyComponent = () => 

    Hello

    ;

    When used inside React component and passed to Render.

提交回复
热议问题