What is React component 'displayName' is used for?

前端 未结 2 699
眼角桃花
眼角桃花 2020-12-30 19:03

I know that is it considered a good practice to name react component by adding a displayName property, but not sure I know why. In react docs, it say:

2条回答
  •  忘掉有多难
    2020-12-30 19:59

    I have always set displayName to the same name as the variable I am assigning it to. This is would only been used in development builds as it is removed through dead-code elimination on production builds and should not be relied on within your application.

    As for where it is used, that is mainly within react error messaging. This is why it is mentioned to be valuable for debugging. If no name can be derived the error messages default to say Component which is extremely difficult to debug, when you have any more than 1 component in your project.

    Here are a few error messages that reference displayName in the react source:

    Invalid Return

    Inline style error

提交回复
热议问题