Skip to content

Instantly share code, notes, and snippets.

@kjrocker
Last active May 3, 2019 17:12
Show Gist options
  • Save kjrocker/68ab64bd92b5d90615bcf423da9bbed6 to your computer and use it in GitHub Desktop.
Save kjrocker/68ab64bd92b5d90615bcf423da9bbed6 to your computer and use it in GitHub Desktop.
Quickly create a classic context HOC from a context
// 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} />;
};
@kjrocker
Copy link
Author

kjrocker commented May 3, 2019

InferableComponentEnhancer is pulled from recompose, but lots of libraries have an equivalent type.

From the recompose docs:

Injects props and removes them from the prop requirements. Will not pass through the injected props if they are passed in during render.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment