compose-providers
Compose your React provider components to avoid boilerplates.
Install
npm i compose-providers
Usage
Assume you're using serveral libraries that requires using their provider components like:
- react-error-boundary
- react-intl
- react-query
- react-router
and you have to nest them around your app to get everything work
render(
<ErrorBoundary>
<QueryClientProvider client={queryClient}>
<IntlProvider locale={locale} messages={messages}>
<BrowserRouter>{/** your app */}</BrowserRouter>
</IntlProvider>
</QueryClientProvider>
</ErrorBoundary>,
)
With compose-providers
you can compose those providers into one component so that you get rid of all those boilerplates.
import { composeProviders } from "compose-providers"
const AppContainer = composeProviders([
ErrorBoundary,
[
QueryClientProvider,
{
client: queryClient,
},
],
[
IntlProvider,
{
locale,
messages,
},
],
BrowserRouter,
])
render(<AppContainer>{/** your app */}</AppContainer>)
This is especiall useful when you are writing tests and need to combine different providers as wrappers to get your component work:
import { render } from "@testing-library/react"
const wrapper = composeProviders([QueryClientProvider, BrowserRouter])
render(<RouteComponent />, {
wrapper,
})
import { render } from "@testing-library/react"
const wrapper = composeProviders([QueryClientProvider, IntlProvider])
render(<IntlComponent />, {
wrapper,
})
License
MIT © SevenOutman