Created
February 4, 2022 18:38
-
-
Save erikunha/c4f29a1e5a804ad4909f9e85b92b58f3 to your computer and use it in GitHub Desktop.
React Composer
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
import { ReactNode } from 'react'; | |
export type ComponentWithChildren = (props: { | |
children: ReactNode; | |
}) => JSX.Element; | |
export const composer = (...providers: ComponentWithChildren[]) => { | |
const ComposedComponents: ComponentWithChildren = ({ children }) => { | |
return ( | |
<> | |
{providers.reduceRight( | |
(child, Provider) => ( | |
<Provider>{child}</Provider> | |
), | |
children, | |
)} | |
</> | |
); | |
}; | |
return ComposedComponents; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment