Created
August 27, 2022 10:08
-
-
Save NuroDev/ae5a418343c72fa71ba0df77443f8ffe to your computer and use it in GitHub Desktop.
π€ Compose Providers β Cleaner pattern for handling multiple context providers
This file contains hidden or 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
// https://twitter.com/bidah/status/1563197138854588417 | |
import reduceRight from 'lodash/reduceRight'; | |
import type { ElementType, ReactNode } from 'react'; | |
interface ComposeProvidersProps { | |
components: Array<ElementType>; | |
children: ReactNode; | |
} | |
export function ComposeProviders({ components, children }: ComposeProvidersProps): JSX.Element { | |
return ( | |
<> | |
{reduceRight( | |
components, | |
(memo, Component) => ( | |
<Component>{memo}</Component> | |
), | |
children, | |
)} | |
</> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment