Skip to content

Instantly share code, notes, and snippets.

@erikunha
Created February 4, 2022 18:38
Show Gist options
  • Save erikunha/c4f29a1e5a804ad4909f9e85b92b58f3 to your computer and use it in GitHub Desktop.
Save erikunha/c4f29a1e5a804ad4909f9e85b92b58f3 to your computer and use it in GitHub Desktop.
React Composer
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