[React] Add guidelines for setting displayName on higher-order components
See original GitHub issueHigher-order components are functions that take a Component as an argument and return a new Component that renders the passed-in Component. When doing this, it is nice to set the displayName
on the generated Component to make it obvious (e.g. in dev tools) to understand what is going on. I prefer the convention of higherOrderComponent(WrappedComponent)
.
For example, if I have a higher-order component called withFoo
and a component that is wrapped by this called Bar
:
export default withFoo(Bar);
the displayName
on the generated component would be set to withFoo(Bar)
, e.g.:
export default function withFoo(Component) {
function WithFoo(props) {
// TODO: do something special here.
return <Component {...this.props} />;
}
WithFoo.displayName = `withFoo(${Component.displayName || Component.name}`;
return WithFoo;
}
Issue Analytics
- State:
- Created 7 years ago
- Reactions:4
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Higher-Order Components - React
A higher-order component (HOC) is an advanced technique in React for reusing component logic. HOCs are not part of the React API, per...
Read more >Introduction to Higher-Order Components in React by example
In this blog, we will learn about High-Order Components and how we use them in React. It also covers coding and debugging Higher-order...
Read more >Higher-Order Components In React - Smashing Magazine
In this tutorial, we are going to learn about higher-order components, the syntax of higher-order components, as well as use cases for them....
Read more >component definition is missing display name on HOC
First: Fix order of your functional component declaration. Second: setting displayName to the component returned from HOC
Read more >Higher-Order Components · Styleguide JavaScript
Display Name. Use a composite of the higher-order component's (HOC) name and the passed-in component's name as the displayName on the generated component....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Related: https://github.com/airbnb/react-with-styles/issues/32#issuecomment-255425391
For testing the underlying component with Enzyme, you probably want to use
.dive()
.I hope that helps! Let me know if you have any questions.
Cool, thanks for the response!