Coder Social home page Coder Social logo

cartmanishere / hooks-combined-reducers Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sagarbajpai/hooks-combined-reducers

0.0 1.0 0.0 1.76 MB

Its a custom hook to combine all your useReducer hooks in return for one global state container with one dispatch function. You can use it at the top-level component and pass dispatch function (and state) down via React's Context API with Provider and Consumer/useContext.

License: MIT License

HTML 6.42% JavaScript 92.69% CSS 0.89%

hooks-combined-reducers's Introduction

hooksCombinedReducers React Hook

Github issues Github forks Github stars Github license Twitter

Its a custom hook to combine all your useReducer hooks in return for one global state container with one dispatch function. You can use it at the top-level component & it will pass a dispatch and state functions down with the help of React's Context API with Provider and Consumer (or useContext).

Installation

npm install hooks-combined-reducers

Usage

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

Create your reducer first, in some file Table.js inside any Reducer folder

const tableState = {
  selected: []
};

const tableReducer = (state, action) => {
  switch (action.type) {
    case "TABLE_CHECK_BOXES":
      return {
        ...state,
        selected: action.value,
      };
    default:
      return state;
  }
};
export { tableState, tableReducer };

Inside some file named as Context.js:

import React, { useReducer } from "react";
import hooksCombinedReducers from 'hooks-combined-reducers';
import { tableState, tableReducer } from "./Reducers/Table";

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

function HookProvider(props) {
  const [state, dispatch] = hooksCombinedReducers({
    table: useReducer(tableReducer, tableState),
  });
  return (
    <DispatchContext.Provider value={dispatch}>
      <StateContext.Provider value={state}>
        {props.children}
      </StateContext.Provider>
    </DispatchContext.Provider>
  );
}
export { HookProvider };

Inside root file App.js :

import React from 'react';
import { HookProvider } from "./Context";
import AppComponent from "./AppComponent";

function App(){
  return (
    <HookProvider>
      <AppComponent />
    </HookProvider>
  )
}

export default App;

Inside some component with name AppComponent.js:

import React, { useEffect, useContext } from "react";
import { StateContext, DispatchContext } from "./Context";

function AppComponent() {
  const state = useContext(StateContext);
  const dispatch = useContext(DispatchContext);

  useEffect(() => {
    dispatch({ type: "TABLE_CHECK_BOXES", value: [0] });
  }, []);
  return <div>Hello There!</div>;
}

export default AppComponent;

Contribute

  • git clone https://github.com/SagarBajpai/hooks-combined-reducers.git
  • cd hooks-combined-reducers
  • npm install

Want to know the developer behind this package?

LinkedIn Facebook Twitter

hooks-combined-reducers's People

Contributors

sagarbajpai avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.