Forked from kentcdodds/create-hoc-from-render-prop.js
Created
April 27, 2018 03:48
-
-
Save arekbartnik/269f72b6aa1fdff16677da6d80ec1eae to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// handy method to create a Higher Order Component out of a | |
// Render Prop Component (like a Context.Consumer). | |
// handles, statics, displayName, refs, and value forwarding | |
function createHOCFromRenderProp({prop, Consumer}) { | |
return Component => { | |
function Wrapper(props, ref) { | |
return ( | |
<Consumer> | |
{value => <Component {...{...props, [prop]: value, ref}} />} | |
</Consumer> | |
) | |
} | |
const upperProp = prop.slice(0, 1).toUpperCase() + prop.slice(1) | |
const componentName = Component.displayName || Component.name | |
Wrapper.displayName = `with${upperProp}(${componentName})` | |
// import hoistNonReactStatics from 'hoist-non-react-statics' | |
return hoistNonReactStatics(React.forwardRef(Wrapper), Component) | |
} | |
} | |
const withToggle = createHOCFromRenderProp({ | |
prop: 'toggle', | |
Consumer: Toggle.Consumer, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment