Skip to content

Commit f450adb

Browse files
committed
Add a section "Displaying a custom name in DevTools"
1 parent b6c0a51 commit f450adb

6 files changed

Lines changed: 44 additions & 11 deletions

File tree

content/blog/2018-03-29-react-v-16-3.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ HOCs typically [pass props through](/docs/higher-order-components.html#conventio
5252
The new `forwardRef` API solves this problem by providing a way for us to intercept a `ref` and forward it as a normal prop:
5353
`embed:16-3-release-blog-post/forward-ref-example.js`
5454

55+
[Learn more about the forwardRef API here.](/docs/forwarding-refs.html)
56+
5557
## Component Lifecycle Changes
5658

5759
React's class component API has been around for years with little change. However, as we add support for more advanced features (such as [error boundaries](/docs/react-component.html#componentdidcatch) and the upcoming [async rendering mode](/blog/2018/03/01/sneak-peek-beyond-react-16.html)) we stretch this model in ways that it was not originally intended.

content/docs/forwarding-refs.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,18 @@ This means that refs intended for our `FancyButton` component will actually be a
2020
Fortunately, we can explicitly forward refs to the inner `FancyButton` component using the `React.forwardRef` API. `React.forwardRef` accepts a render function that receives `props` and `ref` parameters and returns a React node. For example:
2121
`embed:forwarding-refs/log-props-after.js`
2222

23+
## Displaying a custom name in DevTools
24+
25+
`React.forwardRef` accepts a render function. React DevTools uses this function to determine what to display for the ref forwarding component.
26+
27+
For example, the following component will appear as "*ForwardRef*" in the DevTools:
28+
29+
`embed:forwarding-refs/wrapped-component.js`
30+
31+
If you name the render function, DevTools will include its name (e.g. "*ForwardRef(myFunction)*"):
32+
33+
`embed:forwarding-refs/wrapped-component-with-function-name.js`
34+
35+
You can even set the function's `displayName` property to include the component you're wrapping:
36+
37+
`embed:forwarding-refs/customized-display-name.js`
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function logProps(Component) {
2+
class LogProps extends React.Component {
3+
// ...
4+
}
5+
6+
function forwardRef(props, ref) {
7+
return <LogProps {...props} forwardedRef={ref} />;
8+
}
9+
10+
// Give this component a more helpful display name in DevTools.
11+
// e.g. "ForwardRef(logProps(MyComponent))"
12+
// highlight-range{1-2}
13+
const name = Component.displayName || Component.name;
14+
forwardRef.displayName = `logProps(${name})`;
15+
16+
return React.forwardRef(forwardRef);
17+
}

examples/forwarding-refs/log-props-after.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,7 @@ function logProps(Component) {
1919
// We can pass it along to LogProps as a regular prop, e.g. "forwardedRef"
2020
// And it can then be attached to the Component.
2121
// highlight-range{1-3}
22-
function forwardRef(props, ref) {
22+
return React.forwardRef((props, ref) => {
2323
return <LogProps {...props} forwardedRef={ref} />;
24-
}
25-
26-
// These next lines are not necessary,
27-
// But they do give the component a better display name in DevTools,
28-
// e.g. "ForwardRef(logProps(MyComponent))"
29-
// highlight-range{1-2}
30-
const name = Component.displayName || Component.name;
31-
forwardRef.displayName = `logProps(${name})`;
32-
33-
return React.forwardRef(forwardRef);
24+
});
3425
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const WrappedComponent = React.forwardRef(
2+
function myFunction(props, ref) {
3+
return <LogProps {...props} forwardedRef={ref} />;
4+
}
5+
);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const WrappedComponent = React.forwardRef((props, ref) => {
2+
return <LogProps {...props} forwardedRef={ref} />;
3+
});

0 commit comments

Comments
 (0)