Skip to content

Instantly share code, notes, and snippets.

@devjmetivier
Last active April 16, 2020 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devjmetivier/ff3db2e883721c699a3f7608b64eddab to your computer and use it in GitHub Desktop.
Save devjmetivier/ff3db2e883721c699a3f7608b64eddab to your computer and use it in GitHub Desktop.
Combines context providers into single provider to wrap 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