Less boilerplate when create a new reducer on Redux
Method:
'use strict'
const createReducer = (initialState, actionHandlers) => {
return (state = initialState, action) => {
return actionHandlers.hasOwnProperty(action.type)
? actionHandlers[action.type](state, action)
: state
}
}
export default createReducer
Usage:
'use strict'
import { ADD_COUNTER, REMOVE_COUNTER, INCREMENT, DECREMENT } from './actions'
import createReducer from '../create-reducer'
export const initialState = []
export default createReducer(initialState, {
[ADD_COUNTER]: (state, action) => state.concat(0),
[REMOVE_COUNTER]: (state, action) => state.filter((_, i) => i !== action.index),
[INCREMENT]: (state, action) => state.map((c, i) => i === action.index ? c + 1 : c),
[DECREMENT]: (state, action) => state.map((c, i) => i === action.index ? c - 1 : c)
})
Ahhhhhh eles tem q se acostumr a nao usar uhauhauhiaiuhuha
Mas ficou lindo man! Congratz!