Last active
April 15, 2023 04:18
-
-
Save buster95/c4dd199c03befec0b8c08cf12f6747b9 to your computer and use it in GitHub Desktop.
React combine components
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
// 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