美文网首页
reactComponent.displayName

reactComponent.displayName

作者: stonehank | 来源:发表于2018-07-18 09:21 被阅读0次

displayName:定义调试时的组件name

例如:

 function withHOC(WrapComponent) {
   // 此处未定义名称或者希望动态定义名称
   return class extends React.Component {
     // 定义displayName
     static displayName = `withHOC(${WrapComponent.displayName || WrapComponent.name})`;
     render(){
       console.log("inside HOC")
       return <WrapComponent {...this.props } />;
     }
   }
 }

 App = withHOC(App);

如果未定义displayName,那么进行调试的时候,就会显示如下:

// react自动定义名称
|---_class2
  |---App
    ...

定义displayName后,显示如下:

|---withHOC(App)
  |---App
    ...

相关文章

  • reactComponent.displayName

    displayName:定义调试时的组件name 例如: 如果未定义displayName,那么进行调试的时候,就...

网友评论

      本文标题:reactComponent.displayName

      本文链接:https://www.haomeiwen.com/subject/rzgnpftx.html