Instead of
[ACTION_TYPE]: (state, action) => {
const newData = action.payload.data.map(() => {
/* ... */
}).filter(() => {
/* ... */
})
/* ... */
return {
...state,
/* ... */
}
}Use
import { formatData } from './helpers'
/* ... */
[ACTION_TYPE]: (state, action) => {
return {
...state,
data: formatData(action.payload)
}
}export function createReducer(initialState, handlers) {
return (state = initialState, action) => {
const handler = handlers[action.type];
if (!handler) return state;
return { ...state, ...handler(state, action) };
};
}