简体   繁体   English

为什么 React 开发工具将我的组件显示为匿名?

[英]Why does React dev tools show my component as Anonymous?

React dev tools works perfectly (properly shows the name of the component in components tab) when you have something like:当您有以下内容时,React 开发工具可以完美运行(在组件选项卡中正确显示组件的名称):

const MyComponent = ...
export { MyComponent }

But if you change it to inline exporting:但是,如果您将其更改为内联导出:

export const MyComponent = ...

it displays the component name as Anonymous.它将组件名称显示为匿名。

Is something wrong with inline exporting in general?一般内联导出有什么问题吗?

For inline exporting you need to manually specify the displayName property (I know, it's a pain).对于内联导出,您需要手动指定 displayName 属性(我知道,这很痛苦)。

So you do所以你也是

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

    MyComponent.displayName = "MyComponent";

If it's still not resolved as it wasn't for me, I found a workaround, I added -如果它仍然没有解决,因为它不适合我,我找到了一个解决方法,我补充说 -

devtool: 'eval-cheap-module-source-map'

to my webpack.config.js and make sure to start the webpack build after adding the property.添加到我的webpack.config.js并确保在添加属性后启动 webpack 构建。

Before adding the devtool.在添加 devtool 之前。 前

After adding the devtool.添加开发工具后。 后

source: https://webpack.js.org/configuration/devtool/来源: https : //webpack.js.org/configuration/devtool/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的组件在 React 组件开发工具中显示为匿名 - Why does my component show as Anonymous in React components dev tool chrome开发者工具为什么显示扩展ID打击了iFrame? - Why does chrome dev tools show the extension ID blow the iFrames? 为什么在chrome开发工具中将对命名函数的调用视为匿名? - Why is the invocation of a named function considered anonymous in chrome dev tools? 为什么我的 React 组件会自动重新渲染? - Why does my React component rerender automatically? 为什么 Redux 商店显示我的 state,但反应组件说它未定义? - Why does Redux store show my state, but react component says its undefined? React 开发工具显示数据状态,控制台显示为空 - React dev tools show data state, console shows empty 在chrome开发工具中记录匿名功能 - Logging anonymous function in the chrome dev tools 为什么 graphql 不将我的数据注入反应组件道具? - Why does graphql not inject my data into react component prop? 为什么我的 React Native 组件不存在 this.props.onLayout? - Why does this.props.onLayout not exist for my React Native component? 为什么当 Set 状态改变时我的 React 组件没有重新渲染? - Why my React component does not rerender when Set state was changed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM