Last active
May 3, 2019 17:12
-
-
Save kjrocker/68ab64bd92b5d90615bcf423da9bbed6 to your computer and use it in GitHub Desktop.
Quickly create a classic context HOC from a context
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
// Generate higher order components from contexts | |
const createContextEnhancer = <P extends any>( | |
key: string, | |
context: React.Context<any> | |
): InferableComponentEnhancer<P> => BaseComponent => props => { | |
const contextValue = React.useContext(context); | |
const newProps = { [key]: contextValue, ...props }; | |
return <BaseComponent {...newProps} />; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
InferableComponentEnhancer
is pulled fromrecompose
, but lots of libraries have an equivalent type.From the
recompose
docs: