Skip to content

Instantly share code, notes, and snippets.

@jacksteamdev
Last active June 3, 2020 22:22
Show Gist options
  • Save jacksteamdev/2f8b08432677f699866602a28f6c7d86 to your computer and use it in GitHub Desktop.
Save jacksteamdev/2f8b08432677f699866602a28f6c7d86 to your computer and use it in GitHub Desktop.
Compose for React Context
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