Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/morewings/react-omni-provider

Omni Provider allows to merge multiple React Provider components into one
https://github.com/morewings/react-omni-provider

context context-api emotion global-state react styled-components styled-components-theme theme-provider theme-switcher

Last synced: 11 days ago
JSON representation

Omni Provider allows to merge multiple React Provider components into one

Awesome Lists containing this project

README

        

[![Release](https://github.com/morewings/react-omni-provider/actions/workflows/merge-jobs.yml/badge.svg)](https://github.com/morewings/react-omni-provider/actions/workflows/merge-jobs.yml)
[![types included](https://img.shields.io/github/package-json/types/morewings/react-omni-provider)](https://github.com/morewings/react-omni-provider)
[![npm version](https://badge.fury.io/js/react-omni-provider.svg)](https://www.npmjs.com/package/react-omni-provider)
[![zero dependencies](https://img.shields.io/badge/zero-dependencies-teal)](https://github.com/morewings/css-vars-hook)
[![npm bundle size](https://deno.bundlejs.com/badge?q=react-omni-provider@latest&config={%22esbuild%22:{%22external%22:[%22react%22,%22react-dom%22]}})](https://bundlejs.com/?q=react-omni-provider@latest&config={%22esbuild%22:{%22external%22:[%22react%22,%22react-dom%22]}})
[![Maintainability](https://api.codeclimate.com/v1/badges/e96c5a81106945e6bfed/maintainability)](https://codeclimate.com/github/morewings/react-omni-provider/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/e96c5a81106945e6bfed/test_coverage)](https://codeclimate.com/github/morewings/react-omni-provider/test_coverage)

# React Omni Provider

React Omni Provider

**React Omni Provider** is an utility component which allows to merge multiple Providers (wrapper components that create context or another global state).

## Install

```shell
npm i react-omni-provider
```

## Use
Provider component is a very popular React development pattern. Provider allows to create global state for your application. The problem is that today, plenty of libraries are using such approach. So after some time, your root file will probably look like the one below. Which sadly reminds infamous callback hell.

### Before

```tsx
import { ThemeProvider } from '@emotion/react'
import { Provider } from 'react-redux'
import { AuthProvider } from 'authorization-package';
import { ConsentProvider } from 'consent-package';
import { NotificationProvider } from '@/lib/Notification';
import { ModalProvider } from '@/lib/Modal';
import { FeatureProvider } from '@/lib/Feature';
// etc

export const App: FC = ({children}) => {
return (







{children}







);
};
```

### After

React Omni Provider allows you to use just one Provider component instead. You can provide a list of used providers as an array. Providers are applied from left to right. So the first item in the list becomes the first wrapper.

```tsx
import { OmniProvider } from 'react-omni-provider';
import { ProviderA } from 'ProviderA';
import { ProviderB } from 'ProviderB';
import { ProviderC } from 'ProviderC';

const providerConfig = [
// you can add just provider component
ProviderA,
// or attach props to it
[ProviderB, {prop: 'value'}],
ProviderC
]

export const App: FC = ({children}) => {
// same as {children}
return (

{children}

);
};
```

## Higher-order Component

There is also a higher-order component available with the same functionality.

```tsx
import type {ComponentProps} from 'react';
import { withOmniProvider } from 'react-omni-provider';
import { ProviderA } from 'ProviderA';
import { ProviderB } from 'ProviderB';
import { ProviderC } from 'ProviderC';

const providerConfig = [
// you can add just provider component
ProviderA,
// or attach props to it
[ProviderB, {prop: 'value'} as ComponentProps],
ProviderC
]

const App: FC = ({children}) => {
return (
{children}
);
};

export const WrappedApp = withOmniProvider(providerConfig)(App)
```