handle actions without using switch statements
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
dist
lib
src
test
.babelrc
.gitattributes
.gitignore
.node-version
LICENSE
README.md
index.d.ts
index.html
package-lock.json
package.json
tsconfig.json
webpack.config.js

README.md

redux-action-handler

Install

$ npm install redux-action-handler --save

Usage

reducer.js

import * as types from '../constants/ActionTypes';
import ActionHandler from 'redux-action-handler';

const initialState = {
  count: 0
};

const handler = new ActionHandler(initialState);

handler.addCase(types.INCREMENT, (state, action) => {
  return Object.assign({}, state, { count: state.count + 1 });
});

handler.addCase(types.DECREMENT, (state, action) => {
  return Object.assign({}, state, { count: state.count - 1 });
});

export default handler.create();

equivalent to

import * as types from '../constants/ActionTypes';
const initialState = {
  count: 0
};

export default (state = initialState, actions) => {
  switch (actions.type) {
    case: types.INCREMENT:
      return Object.assign({}, state, { count: state.count + 1 });
    case: types.DECREMENT:
      return Object.assign({}, state, { count: state.count - 1 });
    default:
      return state;
  }
}