Warm tip: This article is reproduced from serverfault.com, please click

javascript-为什么React开发工具将我的组件显示为Anonymous?

(javascript - Why does React dev tools show my component as Anonymous?)

发布于 2020-03-23 18:44:00

当你遇到以下情况时,React开发工具可以完美运行(正确地在“组件”选项卡中显示组件的名称):

const MyComponent = ...
export { MyComponent }

但是,如果将其更改为内联导出:

export const MyComponent = ...

它将组件名称显示为“匿名”。

一般而言,内联导出有什么问题吗?

Questioner
Boris Parfenenkov
Viewed
1
Iskandar Reza 2020-03-24 02:50:20

对于内联导出,你需要手动指定displayName属性(我知道,这很痛苦)。

所以你也是

    export const MyComponent = () => {
      //stuff happens here
    }

    MyComponent.displayName = "MyComponent";