npm.io
1.1.0 • Published 6 years ago

vanilla-create-reducer

Licence
ISC
Version
1.1.0
Deps
0
Size
5 kB
Vulns
0
Weekly
0

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

Keywords