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