Merge pull request #767 from koba04/customizing-display-name-ref-forwarding

Add a section "Customizing Name in DevTools" in Forwarding Refs
This commit is contained in:
Brian Vaughn
2018-04-11 08:17:28 -07:00
committed by GitHub
6 changed files with 44 additions and 11 deletions

View File

@@ -0,0 +1,17 @@
function logProps(Component) {
class LogProps extends React.Component {
// ...
}
function forwardRef(props, ref) {
return <LogProps {...props} forwardedRef={ref} />;
}
// Give this component a more helpful display name in DevTools.
// e.g. "ForwardRef(logProps(MyComponent))"
// highlight-range{1-2}
const name = Component.displayName || Component.name;
forwardRef.displayName = `logProps(${name})`;
return React.forwardRef(forwardRef);
}

View File

@@ -19,16 +19,7 @@ function logProps(Component) {
// We can pass it along to LogProps as a regular prop, e.g. "forwardedRef"
// And it can then be attached to the Component.
// highlight-range{1-3}
function forwardRef(props, ref) {
return React.forwardRef((props, ref) => {
return <LogProps {...props} forwardedRef={ref} />;
}
// These next lines are not necessary,
// But they do give the component a better display name in DevTools,
// e.g. "ForwardRef(logProps(MyComponent))"
// highlight-range{1-2}
const name = Component.displayName || Component.name;
forwardRef.displayName = `logProps(${name})`;
return React.forwardRef(forwardRef);
});
}

View File

@@ -0,0 +1,5 @@
const WrappedComponent = React.forwardRef(
function myFunction(props, ref) {
return <LogProps {...props} forwardedRef={ref} />;
}
);

View File

@@ -0,0 +1,3 @@
const WrappedComponent = React.forwardRef((props, ref) => {
return <LogProps {...props} forwardedRef={ref} />;
});