Last active
April 25, 2019 09:33
-
-
Save ricsam/b1c7fb6a69c42183b5b712fd86d448da to your computer and use it in GitHub Desktop.
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 hoist from 'hoist-non-react-statics'; | |
import React from 'react'; | |
const { Provider, Consumer } = React.createContext({}); | |
export const PropsProvider = ({ props, children }) => { | |
return ( | |
<Consumer> | |
{(providedProps) => { | |
const newProps = { ...providedProps, ...props }; | |
return <Provider value={newProps}>{children}</Provider>; | |
}} | |
</Consumer> | |
); | |
}; | |
export const withProps = (Component) => { | |
const WithProps = (ownProps) => { | |
return ( | |
<Consumer> | |
{(providedProps) => { | |
const allProps = { ...providedProps, ...ownProps }; | |
return <Component {...allProps} />; | |
}} | |
</Consumer> | |
); | |
}; | |
hoist(WithProps, Component); | |
return WithProps; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment