Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

use-state-with-callback

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-state-with-callback

[![Build Status](https://travis-ci.org/the-road-to-learn-react/use-combined-reducers.svg?branch=master)](https://travis-ci.org/the-road-to-learn-react/use-combined-reducers) [![Slack](https://slack-the-road-to-learn-react.wieruch.com/badge.svg)](https://s

  • 1.0.6
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7.2K
decreased by-20.73%
Maintainers
1
Weekly downloads
 
Created
Source

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

FAQs

Package last updated on 31 May 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc