Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

component definition is missing display name on HOC

I'm trying to create a higher order component but keep getting this eslint waring.

component definition is missing display name

I tried adding a display name like below but it still complains.

import React from 'react';

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

export default HOC;
like image 770
me-me Avatar asked Feb 09 '19 00:02

me-me


People also ask

What are hoc components?

A higher-order component (HOC) is an advanced technique in React for reusing component logic. HOCs are not part of the React API, per se. They are a pattern that emerges from React's compositional nature. Concretely, a higher-order component is a function that takes a component and returns a new component.

What is DisplayName in React?

DisplayName allows you to name your component. This name is used by React in debugging messages.

What is React component?

Components are independent and reusable bits of code. They serve the same purpose as JavaScript functions, but work in isolation and return HTML. Components come in two types, Class components and Function components, in this tutorial we will concentrate on Function components.


1 Answers

Two things you need to correct.

First: Fix order of your functional component declaration.

Second: setting displayName to the component returned from HOC

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

Once you make the above change. You just need to use the HOC like

const MyCompWithHoc = HOC(CompA);

and render it like

<MyCompWithHoc propsA={'A'} {...otherPropsYouWantToPass} />
like image 55
Shubham Khatri Avatar answered Oct 23 '22 11:10

Shubham Khatri