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;
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.
DisplayName allows you to name your component. This name is used by React in debugging messages.
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.
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} />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With