Skip to content

Instantly share code, notes, and snippets.

@dale-french
Created February 3, 2020 11:41
Show Gist options
  • Save dale-french/2267e5d2d56f074a82d74928c63e52e4 to your computer and use it in GitHub Desktop.
Save dale-french/2267e5d2d56f074a82d74928c63e52e4 to your computer and use it in GitHub Desktop.
Compose multiple contexts
function ProviderComposer({ contexts, children }) {
return contexts.reduceRight(
(kids, parent) =>
React.cloneElement(parent, {
children: kids,
}),
children
);
}
function ContextProvider({ children }) {
return (
<ProviderComposer
contexts={[<CheckoutProvider />, <LoginProvider />, <AlertProvider />]}
>
{children}
</ProviderComposer>
);
}
export { ContextProvider };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment