Last active
March 5, 2020 10:59
-
-
Save khusamov/58da3cebde3913aacf3e0b4fdfd48b70 to your computer and use it in GitHub Desktop.
Решение проблемы типа 'Providers Hell'
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, {FunctionComponent, ReactElement, Fragment, cloneElement} from 'react'; | |
/** | |
* Решение проблемы типа 'Providers Hell'. | |
* @link https://gist.github.com/khusamov/58da3cebde3913aacf3e0b4fdfd48b70 | |
*/ | |
const ApplicationProviders: FunctionComponent<{providers: ReactElement[]}> = ( | |
({children, providers}) => { | |
return ( | |
[...providers, <Fragment>{children}</Fragment>] | |
.reverse() | |
.reduce((result, provider) => cloneElement(provider, {children: result})) | |
); | |
} | |
); | |
export default ApplicationProviders; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment