Skip to content
Custom hook to combine all useReducer hooks for one global state container.
Branch: master
Clone or download
Latest commit 603a747 May 16, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.nyc_output add coveralls May 13, 2019
src add coveralls May 13, 2019
.babelrc spread babel May 13, 2019
.gitignore first commit May 13, 2019
.npmignore first commit May 13, 2019
.prettierrc
.travis.yml add coveralls May 13, 2019
LICENSE add coveralls May 13, 2019
README.md Update README.md May 16, 2019
package-lock.json add coveralls May 13, 2019
package.json bump version May 16, 2019
webpack.config.js first commit May 13, 2019

README.md

useCombinedReducers React Hook

Build Status Slack Greenkeeper badge Coverage Status NPM

Custom hook to combine all useReducer hooks for one global state container with one dispatch function. Use at top-level and pass dispatch function (and state) down via React's Context API with Provider and Consumer/useContext.

Installation

npm install use-combined-reducers

Usage

Create a global dispatch function and state object by initializing multiple useReducer hooks in useCombinedReducers:

import React from 'react';
import useCombinedReducers from 'use-combined-reducers';

const App = () => {
  const [state, dispatch] = useCombinedReducers({
    myTodos: React.useReducer(todoReducer, initialTodos),
    myOtherStuff: React.useReducer(stuffReducer, initialStuff),
  });

  const { myTodos, myOtherStuff } = state;

  ...
}

export default App;

You can pass state and dispatch function down via props or React's Context API. Since passing it down with props is straight forward, the approach with context is demonstrated here. In some file:

import React from 'react';

export const StateContext = React.createContext();
export const DispatchContext = React.createContext();

In your top-level React component (or any other component above a component tree which needs managed state):

import React from 'react';
import useCombinedReducers from 'use-combined-reducers';

import { StateContext, DispatchContext } from './somefile.js'; 

const App = () => {
  const [state, dispatch] = useCombinedReducers({
    myTodos: React.useReducer(todoReducer, initialTodos),
    myOtherStuff: React.useReducer(stuffReducer, initialStuff),
  });

  return (
    <DispatchContext.Provider value={dispatch}>
        <StateContext.Provider value={state}>
          <SomeComponent />
        </StateContext.Provider>
    </DispatchContext.Provider>
  );
}

export default App;

In some other component which sits below the state/dispatch providing component:

import React from 'react';

import { StateContext, DispatchContext } from './somefile.js'; 

export default () => {
  const state = React.useContext(StateContext);
  const dispatch = React.useContext(DispatchContext);
  
  const { myTodos, myOtherStuff } = state; 

  return (
    <div>
      ...
    </div>
  );
};

Contribute

  • git clone git@github.com:the-road-to-learn-react/use-combined-reducers.git
  • cd use-combined-reducers
  • npm install
  • npm run test

More

You can’t perform that action at this time.