Skip to content

Instantly share code, notes, and snippets.

@zzdjk6
Created August 21, 2019 23:01
Show Gist options
  • Save zzdjk6/049323e35dd888ad3d28b7c273104768 to your computer and use it in GitHub Desktop.
Save zzdjk6/049323e35dd888ad3d28b7c273104768 to your computer and use it in GitHub Desktop.
Connect To Context
// @flow
import React from 'react';
type MapValuesToProps = (values: any) => any;
export const connectToContext = (context: React$Context<any>) => (mapValuesToProps: MapValuesToProps) => (
Comp: any
) => {
const wrapped = (props: any) => {
return (
<context.Consumer>
{values => {
const extraProps = mapValuesToProps(values);
return <Comp {...props} {...extraProps} />;
}}
</context.Consumer>
);
};
wrapped.displayName = 'connectToContext';
return wrapped;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment