Last active
June 3, 2020 22:22
-
-
Save jacksteamdev/2f8b08432677f699866602a28f6c7d86 to your computer and use it in GitHub Desktop.
Compose for React 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
import React from 'react' | |
/** | |
* Usage: | |
* | |
* <Compose components={[ | |
* [BrowserRouter, browserValue], | |
* [AuthProvider, authValue], | |
* [ThemeProvider, themeValue], | |
* [ChatProvider, chatValue] | |
* ]}> | |
* <App /> | |
* </Compose> | |
*/ | |
interface Props { | |
contexts: Array<[Context<any>, any]> | |
children: ReactNode | |
} | |
export default function Compose(props: Props) { | |
const { contexts = [], children } = props | |
return ( | |
<> | |
{contexts.reduce((acc, [Context, value]) => { | |
return <Context.Provider value={value}>{acc}</Context.Provider> | |
}, children)} | |
</> | |
) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment