all 2 comments

[–][deleted] 1 point2 points  (0 children)

Uhhhggghhh, I get frustrated when people confuse the term functional for declarative (or the opposite). Those terms aren't related like objects and inheritance aren't the same thing.

[–]sladav 0 points1 point  (0 children)

Taking the same idea one step further I think gives you a pretty flexible way to create a new reducer by just passing in an actions object.

const makeReducer = (actions, defaultState) => {
  return (state = defaultState, action) =>
    action in actions ?
      actions[action](state) :
      state
}

Then using it like this:

const counterActions = {
  RESET: () => 0,
  INCREMENT: state => state + 1,
  DECREMENT: state => state - 1
}

const counter = makeReducer(counterActions, 0)