Skip to content

Instantly share code, notes, and snippets.

@egor-xyz
Last active July 25, 2022 13:25
Show Gist options
  • Save egor-xyz/705197015599a774baa82d67ac3bbe44 to your computer and use it in GitHub Desktop.
Save egor-xyz/705197015599a774baa82d67ac3bbe44 to your computer and use it in GitHub Desktop.
Medium. How to combine providers in React
type Providers = [ComponentType<any>, ComponentProps<any>?][];
const combineProviders = (providers: Providers): FC => providers.reduce(
(AccumulatedProviders, [Provider, props = {}]) => ({ children }) => (
<AccumulatedProviders>
<Provider {...props}>
<>{children}</>
</Provider>
</AccumulatedProviders>
),
({ children }) => <>{children}</>
);
export const AllProviders = CombineProviders([
[MuiThemeProvider, { theme }],
[MuiPickersUtilsProvider, { utils: DateFnsUtils }],
[StylesProvider, { injectFirst: true }],
[Provider, { store: configureStore() }],
[Provider1],
[Provider2],
[MyComponent, {name: 'John']
]);
import ReactDOM from 'react-dom';
import { StrictMode } from 'react';
import { AllProviders } from './AllProviders';
ReactDOM.render(
<StrictMode>
<AllProviders>
<App />
</AllProviders>
</StrictMode>,
document.getElementById('root')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment