Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

the-road-to-learn-react/use-combined-reducers

Repository files navigation

Build StatusSlackGreenkeeper badgeCoverage StatusNPM

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 multipleuseReducer hooks inuseCombinedReducers:

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 viaprops orReact'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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp