1.1.1 • Published 5 years ago

react-provide-providers v1.1.1

Weekly downloads
6
License
MIT
Repository
github
Last release
5 years ago

react-provide-providers

NPM version NPM downloads

About

Solve "wrapper hell" problem in React project

How to Install

First, install the library in your project by npm:

$ npm install react-provide-providers

Or Yarn:

$ yarn add react-provide-providers

Getting Started

• Import ProvideProviders from library in your React app, wrap main component and pass providers:

// index.js

import React from 'react';
import ReactDOM from 'react-dom';
import { ProvideProviders } from 'react-provide-providers';

import App from './App';

const Theme = React.createContext();
const Auth = React.createContext();

const providers = {
  theme: <Theme.Provider values={{ primary: "dark" }} />,
  auth: <Auth.Provider values={{ isAuthenticated: true }} />,
  modal: { show: false, timeout: 300 }
};

ReactDOM.render(
  <ProvideProviders providers={providers}>
    <App />
  </ProvideProviders>,
  document.getElementById('root')
);

• Then use useProviders Hook:

// App.js

import React from 'react';
import { useProviders } from 'react-provide-providers';

const App = () => {
  const { theme, auth, modal } = useProviders('theme', 'auth', 'modal');

  return (
    <>
      <p>Primary color: {theme.primary}</p>
      <p>Is authenticated: {auth.isAuthenticated}</p>
      <p>Modal visible: {modal.show}</p>
    </>
  );
};

export default App;

• Or withProviders HOC:

// App.js

import React from 'react';
import { withProviders } from 'react-provide-providers';

const App = props => {
  const { theme, auth, modal } = props;

  return (
    <>
      <p>Primary color: {theme.primary}</p>
      <p>Is authenticated: {auth.isAuthenticated}</p>
      <p>Modal visible: {modal.show}</p>
    </>
  );
};

export default withProviders('theme', 'auth', 'modal')(App);

License

This project is licensed under the MIT License © 2020-present Jakub Biesiada

1.1.2-beta.1

5 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago

1.0.0-beta.2

6 years ago

1.0.0-beta.1

6 years ago