Simple createReducer
in functional style for Ramda library.
npm install redux-ramda-reducer
export default (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
}
Without STATE in handler arguments
import * as R from 'ramda';
import { createReducer } from 'redux-ramda-reducer';
import { COUNTER_ACTIONS } from './constants';
const INITIAL_STATE = {
value: 1,
};
const incrementHandler = () => R.evolve({ value: R.inc });
const decrementHandler = () => R.evolve({ value: R.dec });
const setValueHandler = ({ value }) => R.assoc('value', value);
const HANDLERS = {
[COUNTER_ACTIONS.SET_VALUE]: setValueHandler,
[COUNTER_ACTIONS.INCREMENT]: incrementHandler,
[COUNTER_ACTIONS.DECREMENT]: decrementHandler,
}
export default createReducer(INITIAL_STATE, HANDLERS);
If you need STATE in handler:
...
const setSameValueHandler = () => (state) => R.assoc('value', state.value)(state);
const HANDLERS = {
[COUNTER_ACTIONS.SET_SAME_VALUE]: setSameValueHandler,
}
export default createReducer(INITIAL_STATE, HANDLERS);
This module is MIT licensed.