hooks-combine-reducers
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

Hooks Combine Reducers

Building a complex React app using the useReducer hook can lead to long reducers.

It's better to split reducers & states into seperate files. hooks-combine-reducers accepts multiple reducer files or states and combines them into a single object.

How To Use

Create an initial state:

const initialState = {
  key: value
};

Then create multiple reducer functions:

const reducer1 = (state, action) => {
  switch (action.type) {
    case "ACTION":
      return
    default:
      return state;
  }
};

const reducer2 = (state, action) => {
  switch (action.type) {
    case "ACTION":
      return
    default:
      return state;
  }
};

And finally combine the reducers:

  const [state, dispatch] = React.useReducer(
    combineReducers({
      keyName1: reducer1,
      keyName2: reducer2
    }),
    initialState
  );

Combining State

You may want to separate state into different objects for better organization.

import { combineReducers, combineStates } from 'hooks-combine-reducers';


const state1 = {
  ...
}

const state2 = {
  ...
}

const reducer1 = (state, action) => {
  switch (action.type) {
    case "ACTION":
      return
    default:
      return state;
  }
};

const reducer2 = (state, action) => {
  switch (action.type) {
    case "ACTION":
      return
    default:
      return state;
  }
};

const [state, dispatch] = React.useReducer(
  combineReducers({
    keyName1: reducer1,
    keyName2: reducer2
  }),
  combineStates([state1, state2])
);

Readme

Keywords

none

Package Sidebar

Install

npm i hooks-combine-reducers

Weekly Downloads

0

Version

0.1.0

License

MIT

Unpacked Size

16.5 kB

Total Files

19

Last publish

Collaborators

  • dillonraphael