Skip to content

Instantly share code, notes, and snippets.

@buster95
Last active April 15, 2023 04:18
Show Gist options
  • Save buster95/c4dd199c03befec0b8c08cf12f6747b9 to your computer and use it in GitHub Desktop.
Save buster95/c4dd199c03befec0b8c08cf12f6747b9 to your computer and use it in GitHub Desktop.
React combine components
// combineComponents.tsx
// https://javascript.plainenglish.io/how-to-combine-context-providers-for-cleaner-react-code-9ed24f20225e
import React, { ComponentProps, FC } from 'react';
export const combineComponents = (...components: FC[]): FC => {
return components.reduce(
(AccumulatedComponents, CurrentComponent) => {
return ({ children }: ComponentProps<FC>): JSX.Element => {
return (
<AccumulatedComponents>
<CurrentComponent>{children}</CurrentComponent>
</AccumulatedComponents>
);
};
},
({ children }) => <>{children}</>,
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment