1.1.0 • Published 4 years ago

vanilla-create-reducer v1.1.0

Weekly downloads
1
License
ISC
Repository
github
Last release
4 years ago

create-vanilla-reducer Travis CI badge

Create a reducer from an object of reducer functions keyed by the action type. To be used with any JS framework.

An alternative pattern to the classic Redux style switch case.

import createReducer from "vanilla-create-reducer";

const ACTIONS = {
  toggle: "__toggle__",
  set: "__set__",
};

function toggle(state, action) {
  return { ...state, on: !state.on };
}

function set(state, action) {
  return { ...state, on: action.on };
}

const reducer = createReducer({
  [ACTIONS.toggle]: toggle,
  [ACTIONS.set]: set,
});

To see createReducer in action, check out this React sandbox