All Projects → the-road-to-learn-react → Use Combined Reducers

the-road-to-learn-react / Use Combined Reducers

Licence: mit
Custom hook to combine all useReducer hooks for one global state container.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Use Combined Reducers

Mvvm Redux
MVVM Redux is a lightweight lib to help you apply the redux concepts in your project based in MVVM.
Stars: ✭ 30 (-56.52%)
Mutual labels:  state-management
Duix
A State Manager focused on KISS and Pareto's Principle
Stars: ✭ 48 (-30.43%)
Mutual labels:  state-management
Yewdux
Redux-like state containers for Yew apps
Stars: ✭ 58 (-15.94%)
Mutual labels:  state-management
Regie
An observable state management tool for vanilla JS applications based on Proxies
Stars: ✭ 33 (-52.17%)
Mutual labels:  state-management
Rxstate
Simple opinionated state management library based on RxJS
Stars: ✭ 46 (-33.33%)
Mutual labels:  state-management
Mount
managing Clojure and ClojureScript app state since (reset)
Stars: ✭ 1,051 (+1423.19%)
Mutual labels:  state-management
Concent
State management that tailored for react, it is simple, predictable, progressive and efficient.
Stars: ✭ 882 (+1178.26%)
Mutual labels:  state-management
React Composition Api
🎨 Simple React state management. Made with @vue/reactivity and ❤️.
Stars: ✭ 67 (-2.9%)
Mutual labels:  state-management
Moviescatalogflutter
A flutter app sample using different animations with Rx approach Bloc Pattern and Provider
Stars: ✭ 47 (-31.88%)
Mutual labels:  state-management
Alveron
Elm & Reason inspired state management for React
Stars: ✭ 57 (-17.39%)
Mutual labels:  state-management
Use Global Context
A new way to use “useContext” better
Stars: ✭ 34 (-50.72%)
Mutual labels:  state-management
React Values
A set of tiny React components for handling state with render props.
Stars: ✭ 1,025 (+1385.51%)
Mutual labels:  state-management
React Context Hook
A React.js global state manager with Hooks
Stars: ✭ 50 (-27.54%)
Mutual labels:  state-management
Mini Rx Store
Lightweight Redux Store based on RxJS
Stars: ✭ 32 (-53.62%)
Mutual labels:  state-management
Muster
A universal data layer for components and services
Stars: ✭ 59 (-14.49%)
Mutual labels:  state-management
Useredcontext
A Flux/Redux replacer - written with useContext and useReducer hooks.
Stars: ✭ 20 (-71.01%)
Mutual labels:  state-management
Royjs
Royjs is only 4.8kb mvvm framework for React
Stars: ✭ 49 (-28.99%)
Mutual labels:  state-management
Vue State Management Alternative
Vuex state management alternative for both Vue 1.x & 2.x
Stars: ✭ 67 (-2.9%)
Mutual labels:  state-management
React Control Center
without redux、mobx and etc, writing react app with react-control-center is a funny way also, it's definitely worth doing! cc is more than a state management framework ^_^
Stars: ✭ 60 (-13.04%)
Mutual labels:  state-management
Apprun
AppRun is a JavaScript library for developing high-performance and reliable web applications using the elm inspired architecture, events and components.
Stars: ✭ 1,087 (+1475.36%)
Mutual labels:  state-management

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

Optional TS support: npm install @types/use-combined-reducers --save-dev

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 [email protected]:the-road-to-learn-react/use-combined-reducers.git
  • cd use-combined-reducers
  • npm install
  • npm run test

More

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].