Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Combines context providers into single provider to wrapp application
import React, { cloneElement } from 'react';
// import providers
import { ExportedProvider } from './path/to/provider';
function ProviderComposer({ contexts, children }) {
return contexts.reduceRight(
(kids, parent) =>
cloneElement(parent, {
children: kids,
}),
children
);
}
export function ContextProvider({ children }) {
return (
<ProviderComposer
// add providers to array of contexts
contexts={
[
// <ExportedProvider/>,
// <AnotherExportedProvider/>,
]
}
>
{children}
</ProviderComposer>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.