Created
August 21, 2019 23:01
-
-
Save zzdjk6/049323e35dd888ad3d28b7c273104768 to your computer and use it in GitHub Desktop.
Connect To 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
// @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