You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/docs/higher-order-components.md
+10-10Lines changed: 10 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -163,7 +163,7 @@ function withSubscription(WrappedComponent, selectData) {
163
163
}
164
164
```
165
165
166
-
Note that an HOC doesn't modify the input component, nor does it use inheritance to copy its behavior. Rather, an HOC *composes* the original component by *wrapping* it in a container component. An HOC is a pure function with zero side-effects.
166
+
Note that a HOC doesn't modify the input component, nor does it use inheritance to copy its behavior. Rather, a HOC *composes* the original component by *wrapping* it in a container component. A HOC is a pure function with zero side-effects.
167
167
168
168
And that's it! The wrapped component receives all the props of the container, along with a new prop, `data`, which it uses to render its output. The HOC isn't concerned with how or why the data is used, and the wrapped component isn't concerned with where the data came from.
169
169
@@ -173,7 +173,7 @@ Like components, the contract between `withSubscription` and the wrapped compone
173
173
174
174
## Don't Mutate the Original Component. Use Composition.
175
175
176
-
Resist the temptation to modify a component's prototype (or otherwise mutate it) inside an HOC.
176
+
Resist the temptation to modify a component's prototype (or otherwise mutate it) inside a HOC.
177
177
178
178
```js
179
179
functionlogProps(InputComponent) {
@@ -217,7 +217,7 @@ You may have noticed similarities between HOCs and a pattern called **container
217
217
218
218
## Convention: Pass Unrelated Props Through to the Wrapped Component
219
219
220
-
HOCs add features to a component. They shouldn't drastically alter its contract. It's expected that the component returned from an HOC has a similar interface to the wrapped component.
220
+
HOCs add features to a component. They shouldn't drastically alter its contract. It's expected that the component returned from a HOC has a similar interface to the wrapped component.
221
221
222
222
HOCs should pass through props that are unrelated to its specific concern. Most HOCs contain a render method that looks something like this:
// The returned function is an HOC, which returns a component that is connected
272
+
// The returned function is a HOC, which returns a component that is connected
273
273
// to the Redux store
274
274
constConnectedComment=enhance(CommentList);
275
275
```
@@ -297,7 +297,7 @@ The `compose` utility function is provided by many third-party libraries includi
297
297
298
298
## Convention: Wrap the Display Name for Easy Debugging
299
299
300
-
The container components created by HOCs show up in the [React Developer Tools](https://github.com/facebook/react-devtools) like any other component. To ease debugging, choose a display name that communicates that it's the result of an HOC.
300
+
The container components created by HOCs show up in the [React Developer Tools](https://github.com/facebook/react-devtools) like any other component. To ease debugging, choose a display name that communicates that it's the result of a HOC.
301
301
302
302
The most common technique is to wrap the display name of the wrapped component. So if your higher-order component is named `withSubscription`, and the wrapped component's display name is `CommentList`, use the display name `WithSubscription(CommentList)`:
303
303
@@ -322,7 +322,7 @@ Higher-order components come with a few caveats that aren't immediately obvious
322
322
323
323
React's diffing algorithm (called reconciliation) uses component identity to determine whether it should update the existing subtree or throw it away and mount a new one. If the component returned from `render` is identical (`===`) to the component from the previous render, React recursively updates the subtree by diffing it with the new one. If they're not equal, the previous subtree is unmounted completely.
324
324
325
-
Normally, you shouldn't need to think about this. But it matters for HOCs because it means you can't apply an HOC to a component within the render method of a component:
325
+
Normally, you shouldn't need to think about this. But it matters for HOCs because it means you can't apply a HOC to a component within the render method of a component:
326
326
327
327
```js
328
328
render() {
@@ -338,18 +338,18 @@ The problem here isn't just about performance — remounting a component causes
338
338
339
339
Instead, apply HOCs outside the component definition so that the resulting component is created only once. Then, its identity will be consistent across renders. This is usually what you want, anyway.
340
340
341
-
In those rare cases where you need to apply an HOC dynamically, you can also do it inside a component's lifecycle methods or its constructor.
341
+
In those rare cases where you need to apply a HOC dynamically, you can also do it inside a component's lifecycle methods or its constructor.
342
342
343
343
### Static Methods Must Be Copied Over
344
344
345
345
Sometimes it's useful to define a static method on a React component. For example, Relay containers expose a static method `getFragment` to facilitate the composition of GraphQL fragments.
346
346
347
-
When you apply an HOC to a component, though, the original component is wrapped with a container component. That means the new component does not have any of the static methods of the original component.
347
+
When you apply a HOC to a component, though, the original component is wrapped with a container component. That means the new component does not have any of the static methods of the original component.
While the convention for higher-order components is to pass through all props to the wrapped component, it's not possible to pass through refs. That's because `ref` is not really a prop — like `key`, it's handled specially by React. If you add a ref to an element whose component is the result of an HOC, the ref refers to an instance of the outermost container component, not the wrapped component.
397
+
While the convention for higher-order components is to pass through all props to the wrapped component, it's not possible to pass through refs. That's because `ref` is not really a prop — like `key`, it's handled specially by React. If you add a ref to an element whose component is the result of a HOC, the ref refers to an instance of the outermost container component, not the wrapped component.
398
398
399
399
If you find yourself facing this problem, the ideal solution is to figure out how to avoid using `ref` at all. Occasionally, users who are new to the React paradigm rely on refs in situations where a prop would work better.
0 commit comments