Elegant redux reducer
npm i -S match-reducer
The matchReducer accept as first argument an object mapping action types with their respective reducers function. Second argument (optionnal) accept the initialState and a boolean value for the passOnlyPayload property (default to false, pass the whole action to the reducer).
import { matchReducer } from 'match-reducer'
const initialState = { counter: 0, something: 'else' }
const reducer = matchReducer(
{
INC: (state, payload) => ({
counter: state.counter + payload
}),
DEC: (state, payload) => ({
counter: state.counter - payload
})
},
{
initialState,
passOnlyPayload: true
}
)
reducer(null, { type: 'INC', payload: 1 })
// { counter: 1, something: 'else' }
Take the same aguments as matchReducer and return an object with a reducer and camelCased action creators with a type and payload property.
import { makeActionsAndReducer } from 'match-reducer'
const { reducer, actionCreators } = makeActionsAndReducer(
{
INC_SOMETHING: (state, action) => ({
counter: state.counter + action.payload
})
}
)
reducer({ counter: 0 }, actionCreators.incSomething(1) })
// { counter: 1 }
Please open an issue for support.
Please contribute using Github Flow. Create a branch, add commits, and open a pull request.