https://github.com/pmagaz/create-reduxreducer
Creates reducer function for Redux from an object
https://github.com/pmagaz/create-reduxreducer
action actionhandler reducer redux
Last synced: 4 months ago
JSON representation
Creates reducer function for Redux from an object
- Host: GitHub
- URL: https://github.com/pmagaz/create-reduxreducer
- Owner: pmagaz
- License: mit
- Created: 2017-07-03T10:27:41.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-07-04T12:04:20.000Z (over 8 years ago)
- Last Synced: 2025-08-24T18:31:32.400Z (4 months ago)
- Topics: action, actionhandler, reducer, redux
- Language: JavaScript
- Homepage:
- Size: 18.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# create-reduxreducer

[](https://ci.appveyor.com/project/pmagaz/create-reduxreducer)
[](https://coveralls.io/github/pmagaz/create-reduxreducer?branch=master)

`create-reduxreducer` creates a reducer from an object. so you can easyly split your reduce functions as individual function for each actionType instead of use switch case for filter them.
## Table of contents
1. [Installation](#installation)
2. [Usage](#usage)
## Installation
You can install it via npm. If you'll use it in a Isomorphic/Universal App i recommend you installing it as production dependency.
```
$ npm install create-reduxreducer --save
```
## Usage
It only takes two arguments: an object with the action types as a key and the reduce function that will handle the actionType and an initial state.
In this example we define an actionHandlers object and it's reduce functions:
#### myReducer.js
```javascript
import createReduxReducer from 'create-reduxreducer';
import ActionTypes from '../actionTypes';
const actionHandlers = {
[ActionTypes.INPUT_NUMBER]: inputNumber,
[ActionTypes.INPUT_DECIMAL]: inputDecimal,
...
};
function inputNumber(state, action) {
...
}
function inputDecimal(state, action) {
...
}
const myReducer = createReducer(actionHandlers, {});
export { myReducer };
```
Then you can use this reducer as a regular redux reducer and pass it to combineReducers when you configure your store.
If you want to use arrow functions remember to define your actionHandlers object **after** your arrow functions:
```javascript
import createReduxReducer from 'create-reduxreducer';
import ActionTypes from '../actionTypes';
const imputNumber = (state, action) => state;
const imputDecimal = (state, action) => state;
const actionHandlers = {
[ActionTypes.INPUT_NUMBER]: inputNumber,
[ActionTypes.INPUT_DECIMAL]: inputDecimal,
...
};
const myReducer = createReducer(actionHandlers, {});
```
You can pass as initial state whatever you want, regular objets or even ImmutableJs objects:
```javascript
import { Record } from 'immutable';
const DataModel = Record({
id:0,
age: 50,
...
});
export default createReducer(actionHandlers, new DataModel());
```
## Projects using create-reduxreducer
- [react-base](https://github.com/atSistemas/react-base/ (atSistemas React/Redux Isomorphic Platform)
## License
MIT