代码之家  ›  专栏  ›  技术社区  ›  me-me

组件定义缺少hoc上的显示名称

  •  1
  • me-me  · 技术社区  · 6 年前

    我正在尝试创建一个更高阶的组件,但一直得到这个eslint警告。

    组件定义缺少显示名称

    我试图添加一个显示名称,如下所示,但它仍然抱怨。

    import React from 'react';
    
    const HOC = props => (WC) => {
      WC.displayName = 'test'
      return (
        <WC />
      );
    }
    
    export default HOC;
    
    3 回复  |  直到 6 年前
        1
  •  0
  •   Shubham Khatri    6 年前

    const HOC = WC => {
      const MyComp = (props) => {
        return (
            <WC {...props} />
          );
      }
      MyComp.displayName = 'test'
      return MyComp;
    }
    

    const MyCompWithHoc = HOC(CompA);
    

    <MyCompWithHoc propsA={'A'} {...otherPropsYouWantToPass} />
    
        2
  •  0
  •   Cryptic    6 年前

    HOC WC

    HOC.displayName = 'some higher component'

        3
  •  0
  •   helloitsjoe    6 年前

    props https://reactjs.org/docs/higher-order-components.html#convention-wrap-the-display-name-for-easy-debugging

    const HOC = WC => props => {
      WC.displayName = 'test'
      return (
        <WC {...props} />
      );
    }
    

    displayName

    const HOC = WC => {
      WC.displayName = 'test';
      return WC;
    }