https://github.com/steelydylan/redux-action-handler
handle actions without using switch statements
https://github.com/steelydylan/redux-action-handler
Last synced: about 1 year ago
JSON representation
handle actions without using switch statements
- Host: GitHub
- URL: https://github.com/steelydylan/redux-action-handler
- Owner: steelydylan
- License: mit
- Created: 2018-08-01T00:10:35.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-09-01T13:58:03.000Z (almost 8 years ago)
- Last Synced: 2025-02-09T00:02:15.480Z (over 1 year ago)
- Language: TypeScript
- Size: 182 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# redux-action-handler
## Install
```sh
$ npm install redux-action-handler --save
```
## Usage
reducer.js
```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
```js
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;
}
}
```