简体   繁体   English

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

[英]Why does my component show as Anonymous in React components dev tool

I've read a few examples of this but couldn't find an answer that made sense.我已经阅读了一些这样的例子,但找不到有意义的答案。 I have a React component tree which is showing some components with their component names but others as just Anonymous.我有一个 React 组件树,它显示了一些带有组件名称的组件,但其他的只是匿名的。

I can't understand why.我不明白为什么。

The components showing as Anonymous are declared as:显示为匿名的组件声明为:

  export const AddUserButton: React.StatelessComponent<{
  title: string;
  test?: string;
  onClick(): void;
}> = ({ title, test, onClick }) => (
  <CommonUserButton
    title={title}
    test={test}
    onClick={onClick}
  />
);

A simple example and explanation would be good thanks.一个简单的例子和​​解释会很好谢谢。

Babel transpiles components slightly different based on how you declare them. Babel 转译组件的方式根据你声明它们的方式略有不同。 You should have the correct displayName if you declare like this:如果你这样声明,你应该有正确的displayName

// NOTE: React.StatelessComponent<T> is deprecated!
const AddUserButton: React.FunctionComponent<{  
  title: string;
  test?: string;
  onClick(): void;
}> = ({ title, test, onClick }) => (
  <CommonUserButton
    title={title}
    test={test}
    onClick={onClick}
  />
);

export default AddUserButton;

You can also try to set the displayName yourself by doing AddUserButton.displayName = "AddUserButton";您也可以尝试通过执行AddUserButton.displayName = "AddUserButton";自己设置 displayName AddUserButton.displayName = "AddUserButton"; . .

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

相关问题 为什么 React 开发工具将我的组件显示为匿名? - Why does React dev tools show my component as Anonymous? 为什么Chrome开发工具将日期__proto__显示为无效日期? - Why does Chrome Dev Tool show a dates __proto__ as Invalid Date? 为什么我的 React 组件会自动重新渲染? - Why does my React component rerender automatically? 为什么 Redux 商店显示我的 state,但反应组件说它未定义? - Why does Redux store show my state, but react component says its undefined? 为什么我的 React TS 组件库中的某些组件会抛出 Minified React 错误 #321 - Why are some components in my React TS component library throwing Minified React error #321 React 组件 - 为什么可以在 AsyncApp 组件中调度 - React Component - Why can dispatch in AsyncApp Components 为什么 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? 为什么我的反应组件不断重新渲染? - Why does my react component keep re-rendering?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM