reducer-generator
package can help for flux/redux developers to reduce boilerplate when creating actions, action creators and reducers. It can automate part of work and generate actions and action creators by itself.
Example of simple todo app can be found here.
reducer-generator
has only default export - generateReducer()
function. It expects an object as only required argument. Action type and action creator function will be created for each key of this object. Each value should describe reducer case to react on specific action. Reducer case function should receive two arguments: current state and action payload.
// simpleCounter.js
import generateReducer from 'reducer-generator';
const counter = generateReducer({
increment: ({ counterValue, ...rest }) => ({ counterValue: counterValue + 1, ...rest }),
decrement: ({ counterValue, ...rest }) => ({ counterValue: counterValue - 1, ...rest }),
reset: state => ({ ...state, counterValue: 0 })
});
After call above generateReducer()
function will return equivalen for the following object:
{
TYPES: {
increment: 'increment-<unique-increment-id>',
decrement: 'decrement-<unique-decrement-id>',
reset: 'reset-<unique-reset-id>'
},
ACTIONS: {
increment: payload => ({ type: 'increment-<unique-increment-id>', payload }),
decrement: payload => ({ type: 'decrement-<unique-decrement-id>', payload }),
reset: payload => ({ type: 'reset-<unique-reset-id>', payload })
},
reducer: (state, { type, payload }) => {
switch (type) {
case 'increment-<unique-increment-id>':
return { ...state, counterValue: state.counterValue + 1 };
case 'decrement-<unique-decrement-id>':
return { ...state, counterValue: counterValue - 1 };
case 'reset-<unique-reset-id>':
return { ...state, counterValue: 0 };
default:
return state;
}
}
}
By default unique action types will be created using uuid package. Here are some examples of unique action types:
const TYPES = {
increment: "increment-27b7068b-683d-5126-bf3b-914377e27023"
decrement: "decrement-5fce52ab-ab1c-54bf-944b-3a0e6d868b83"
reset: "reset-8ad2e79f-fb96-5dee-b563-eb56b854fe19"
}
In some cases we might need constant values for action types. For instance, when we want to save change history and be able to undo/redo in different session. In this case instead of uuid we can use namespace by passing it as a 2nd argument of generateReducer()
function:
const { TYPES } = generateReducer({
increment: () => { ... },
decrement: () => { ... },
reset: () => { ... }
}, 'counter');
Namespace will be prefixed to each action type const:
const TYPES = {
increment: 'counter.increment',
decrement: 'counter.decrement',
reset: 'counter.reset'
}
We also might want to have some synthetic actions. For instance to use with takeLatest() from redux-saga. To create additional action creators and action types we can pass array of key for those as 3rd argument:
const { TYPES } = generateReducer({
increment: () => { ... },
decrement: () => { ... },
reset: () => { ... }
}, 'counter', [ 'resetAsync' ]);
It will add resetAsync
action type and action creator without changes to reducer:
const TYPES = {
...
resetAsync: 'counter.resetAsync'
}
reducer-generator
provides a simple feature to combine actions. It may help to improve performance in some cases. For instance, when several state updates called synchronously and we're not using any other tool to fix this issue.
const { ACTIONS, reducer } = generateReducer(cases);
const updated = reducer(state, ACTIONS.all(
ACTIONS.reset(),
ACTIONS.increment(),
ACTIONS.increment()
));
Code above performs 3 changes but calls reducer()
only once. It means there will be only one update and even synchronous update will cause only one render.