UNPKG

477 BJavaScriptView Raw
1import produce from "immer";
2/**
3 * ----------------------------------------
4 * createReducer
5 * @param {Object} handlers
6 * @param {Object} init
7 * -------------------------------------s---
8 */
9export default function createReducer(handlers, init) {
10 return (state = init, action) => {
11 const { type, payload } = action;
12 const handler = handlers[type];
13 if (!handler) return state;
14 return produce(state, draft => {
15 handler(draft, payload);
16 });
17 };
18}