npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-provider

v1.0.4

Published

A React Hook that connects your components to multiple React Contexts.

Downloads

12

Readme

use-provider

A React Hook that connects your components to multiple React Contexts.

version minified size downloads build

Installation

npm

npm install use-provider --save

yarn

yarn add use-provider

Usage

With this module you can create multiple React Contexts that are managed internally with useReducer and then connect your components to any of those Contexts through its Providers.

createProvider(initialState, reducer)

// providers.js

import { createProvider } from 'use-provider';

const initialStateA = { counter: 0 };
const reducerA = (state, action) => {
  switch (action.type) {
    case 'increment': {
      return { counter: state.counter + 1 };
    }

    default: {
      return state;
    }
  }
};

const initialStateB = { counter: 0 };
const reducerB = (state, action) => {
  switch (action.type) {
    case 'increment': {
      return { counter: state.counter + 1 };
    }

    default: {
      return state;
    }
  }
};

export const ProviderA = createProvider(initialStateA, reducerA);
export const ProviderB = createProvider(initialStateB, reducerB);

...
// index.js

...

import { ProviderA, ProviderB } from './providers';

ReactDOM.render(
  <ProviderA>
    <ProviderB>
      <App />
    </ProviderB>
  </ProviderA>,
  document.getElementById('root')
);

...

useProvider(provider)

Each time you call useProvider inside a component you are connecting the component to the provider's context. useProvider returns the current state and a dispatch function to send actions that update the context's state.

// App.js

...

import { useProvider } from 'use-provider';
import { ProviderA, ProviderB } from './providers';

const ChildComponent = React.memo(
  function ChildComponent() {
    const [stateA, dispatchA] = useProvider(ProviderA);

    return (
      <div>
        <p>Counter A: {stateA.counter}</p>
        <button onClick={() => dispatchA({ type: 'increment' })}>
          Increment state A
        </button>
      </div>
    );
  }
)

// NOTE: ChildComponent is using React.memo to only rerender when stateA is updated by either
// ChildCompenent or App, otherwise it would also rerender when App component updates stateB.

function App() {
  const [stateA, dispatchA] = useProvider(ProviderA);
  const [stateB, dispatchB] = useProvider(ProviderB);

  return (
    <div>
      <p>Counter A: {stateA.counter}</p>
      <p>Counter B: {stateB.counter}</p>
      <button onClick={() => dispatchA({ type: 'increment' })}>
        Increment state A
      </button>
      <button onClick={() => dispatchB({ type: 'increment' })}>
        Increment state B
      </button>

      <ChildComponent />
    </div>
  );
}


...

combineProviders([providers])

combineProviders lets you combine multiple providers into a single one. To connect components to any of the combined providers you do it calling useProvider(provider) passing the specific provider you want to use from the combined group.

// index.js

const CombinedProvider = combineProviders([
    ProviderA,
    ProviderB,
    ...
])

ReactDOM.render(
  <CombinedProvider>
    <App />
  </CombinedProvider>,
  document.getElementById('root')
);


// MyComponent.js

import { ProviderA } from './providers';

const MyComponent = () => {
  const [stateA, dispatchA] = useProvider(ProviderA);

  return(...);
}

License

MIT