Last active
April 16, 2020 13:10
-
-
Save devjmetivier/ff3db2e883721c699a3f7608b64eddab to your computer and use it in GitHub Desktop.
Combines context providers into single provider to wrap application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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