Last active
July 25, 2022 13:25
-
-
Save egor-xyz/705197015599a774baa82d67ac3bbe44 to your computer and use it in GitHub Desktop.
Medium. How to combine providers in React
This file contains 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
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'] | |
]); |
This file contains 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
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