Skip to content

Instantly share code, notes, and snippets.

@codemile
Created January 2, 2025 16:44
Show Gist options
  • Save codemile/b909c0efa57718c7700c93818e4d44c5 to your computer and use it in GitHub Desktop.
Save codemile/b909c0efa57718c7700c93818e4d44c5 to your computer and use it in GitHub Desktop.
Combines multiple providers into a single provider.
import type {FC, PropsWithChildren} from 'react';
export const combineProviders = (
...providers: Array<FC<PropsWithChildren>>
): FC<PropsWithChildren> => {
return providers.reduce<FC<PropsWithChildren>>(
(AccumulatedProviders, CurrentProvider) => {
const Component: FC<PropsWithChildren> = ({children}) => (
<AccumulatedProviders>
<CurrentProvider>{children}</CurrentProvider>
</AccumulatedProviders>
);
Component.displayName = 'CombinedProviders';
return Component;
},
({children}) => <>{children}</>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment