Skip to content

Instantly share code, notes, and snippets.

@Jimmydalecleveland
Created September 15, 2019 14:28
Show Gist options
  • Save Jimmydalecleveland/fb7eb9171fc09e43f30814817368e948 to your computer and use it in GitHub Desktop.
Save Jimmydalecleveland/fb7eb9171fc09e43f30814817368e948 to your computer and use it in GitHub Desktop.
composing context example that Scott T. found
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