Last active
November 30, 2019 18:02
-
-
Save danalloway/0776fa0e21a00a3fad61a52b882d5baf to your computer and use it in GitHub Desktop.
HOC FTW
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "React" | |
import hoistNonReactStatic from "hoist-non-react-statics" | |
const withHOC = hocProps => WrappedComponent => { | |
// use `hocProps` to configure how the HOC behaves towards it's `WrappedComponent` | |
const { debug } = hocProps | |
if (debug) { | |
console.debug("withHoc is in DEBUG mode") | |
} | |
class WithHOC extends React.Component { | |
render() { | |
const { forwardedRef, ...restProps } = this.props | |
return <WrappedComponent {...restProps} ref={forwardedRef} /> | |
} | |
} | |
WithHOC.displayName = `WithHOC${WrappedComponent.displayName || WrappedComponent.name || "Component"}` | |
hoistNonReactStatic(WithHOC, WrappedComponent) | |
return React.forwardedRef((props, ref) => ( | |
<WithHOC {...props} forwardedRef={ref} /> | |
)) | |
} | |
export default withHOC |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
withHOC({ debug: true })(MyComponent)