Skip to content

Commit 4c8f789

Browse files
committed
Add guideline for displayName in higher-order components
This guideline will help us keep our higher-order components consistent and easy to understand. Fixes airbnb#968.
1 parent eea5f0f commit 4c8f789

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

react/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,28 @@
101101
// good
102102
import Footer from './Footer';
103103
```
104+
- **Higher-order Component Naming**: Use a composite of the higher-order component's name and the passed-in component's name as the `displayName` on the generated component. For example, the higher-order component `withFoo()`, when passed a component `Bar` should produce a component with a `displayName` of `withFoo(Bar)`.
104105
106+
> Why? A component's `displayName` may be used by developer tools or in error messages, and having a value that clearly expresses this relationship helps people understand what is happening.
107+
108+
```jsx
109+
// bad
110+
export default function withFoo(Component) {
111+
return function WithFoo(props) {
112+
return <Component {...props} foo />;
113+
}
114+
}
115+
116+
// good
117+
export default function withFoo(Component) {
118+
function WithFoo(props) {
119+
return <Component {...props} foo />;
120+
}
121+
122+
WithFoo.displayName = `withFoo(${Component.displayName || Component.name}`;
123+
return WithFoo;
124+
}
125+
```
105126
## Declaration
106127

107128
- Do not use `displayName` for naming components. Instead, name the component by reference.

0 commit comments

Comments
 (0)