/reducer-composer

Reducer creator experiment

Primary LanguageJavaScript

reducer-composer

reducer-composer is an experimental reducer helper.

Goals

Install

⚠️ This isn't published. This will not work.

yarn add reducer-composer
# or
npm install reducer-composer

Usage

import { reducerComposer } from "reducer-composer";

const addTodoReducer = reducerComposer(
  (state, action) => action.payload.todoId,
  (state, action) => action.payload.todo
);

const completeTodoReducer = reducerComposer(
  (state, action) => action.payload.todoId,
  (state, action) => state.completionHistory,
  (state, action) => ({
    timesCompleted: state.timesCompleted + 1,
    lastCompleted: new Date()
  })
);

function todoReducer(state, action) {
  switch (action.type) {
    case "TODO/ADD":
      return addTodoReducer(state, action);
    case "TODO/COMPLETE":
      return completeTodoReducer(state, action);
    default:
      return state;
  }
}