how to set displayName in a functional component [React]

后端 未结 3 1847
情书的邮戳
情书的邮戳 2020-12-17 07:37

I know that setting the displayName is sometimes required especially when you\'re dealing with production builds. I want to know how to set it using my function

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-17 08:33

    The docs for displayName say

    The displayName string is used in debugging messages. Usually, you don’t need to set it explicitly because it’s inferred from the name of the function or class that defines the component. You might want to set it explicitly if you want to display a different name for debugging purposes or when you create a higher-order component, see Wrap the Display Name for Easy Debugging for details.

    In your case, you would simply use

    const MyComponent = (props) => { ... }
    
    MyComponent.displayName = 'HeyHey'
    

    Or you can use Object.assign

    const MyComponent =
      Object.assign
        ( props => { ... }
        , { displayName: 'HeyHey' }
        )
    

提交回复
热议问题