Browse Source

Added DevTools name consideration to forwardRef examples

main
Brian Vaughn 7 years ago
parent
commit
c41bb29856
  1. 30
      examples/16-3-release-blog-post/forward-ref-example.js
  2. 24
      examples/forwarding-refs/log-props-after.js

30
examples/16-3-release-blog-post/forward-ref-example.js

@ -1,27 +1,27 @@
function withTheme(Component) { function withTheme(Component) {
// highlight-next-line // Note the second param "ref" provided by React.forwardRef.
function ThemedComponent({forwardedRef, ...rest}) { // We can attach this to Component directly.
// highlight-range{6} // highlight-range{1,5}
function ThemedComponent(props, ref) {
return ( return (
<ThemeContext.Consumer> <ThemeContext.Consumer>
{theme => ( {theme => (
<Component <Component {...props} ref={ref} theme={theme} />
{...rest}
ref={forwardedRef}
theme={theme}
/>
)} )}
</ThemeContext.Consumer> </ThemeContext.Consumer>
); );
} }
// Intercept the "ref" and pass it as a custom prop. // These next lines are not necessary,
// highlight-range{1, 3} // But they do give the component a better display name in DevTools,
return React.forwardRef(function forward(props, ref) { // e.g. "ForwardRef(withTheme(MyComponent))"
return ( // highlight-range{1-2}
<ThemedComponent {...props} forwardedRef={ref} /> const name = Component.displayName || Component.name;
); ThemedComponent.displayName = `withTheme(${name})`;
});
// Tell React to pass the "ref" to ThemedComponent.
// highlight-next-line
return React.forwardRef(ThemedComponent);
} }
// highlight-next-line // highlight-next-line

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

@ -1,4 +1,4 @@
function logProps(WrappedComponent) { function logProps(Component) {
class LogProps extends React.Component { class LogProps extends React.Component {
componentDidUpdate(prevProps) { componentDidUpdate(prevProps) {
console.log('old props:', prevProps); console.log('old props:', prevProps);
@ -6,21 +6,29 @@ function logProps(WrappedComponent) {
} }
render() { render() {
// highlight-next-line
const {forwardedRef, ...rest} = this.props; const {forwardedRef, ...rest} = this.props;
// Assign the custom prop "forwardedRef" as a ref // Assign the custom prop "forwardedRef" as a ref
// highlight-range{1-3} // highlight-next-line
return ( return <Component ref={forwardedRef} {...rest} />;
<WrappedComponent ref={forwardedRef} {...rest} />
);
} }
} }
// Intercept the "ref" and pass it as a custom prop, e.g. "forwardedRef" // Note the second param "ref" provided by React.forwardRef.
// 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} // highlight-range{1-3}
function logPropsForwardRef(props, ref) { function forwardRef(props, ref) {
return <LogProps {...props} forwardedRef={ref} />; return <LogProps {...props} forwardedRef={ref} />;
} }
return React.forwardRef(logPropsForwardRef); // 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);
} }

Loading…
Cancel
Save