Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

compose-providers

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

compose-providers

My awesome typescript library

  • 1.0.1
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

compose-providers

Compose your React provider components to avoid boilerplates.

npm version npm downloads

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,
  // You won't be unfamiliar if you have configured tools like babel/eslint
  [
    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:

// RouteComponent.test.tsx
import { render } from "@testing-library/react"

// const wrapper = (props) => (
//   <QueryClientProvider>
//     <BrowserRouter>{props.children}</BrowserRouter>
//   </QueryClientProvider>
// )

const wrapper = composeProviders([QueryClientProvider, BrowserRouter])

render(<RouteComponent />, {
  wrapper,
})

// IntlComponent.test.tsx
import { render } from "@testing-library/react"

// const wrapper = (props) => (
//   <QueryClientProvider>
//     <IntlProvider>{props.children}</IntlProvider>
//   </QueryClientProvider>
// )

const wrapper = composeProviders([QueryClientProvider, IntlProvider])

render(<IntlComponent />, {
  wrapper,
})

License

MIT © SevenOutman

FAQs

Package last updated on 28 Mar 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc