Skip to content

A utility to create redux reducers from a set of handlers

License

Notifications You must be signed in to change notification settings

dabapps/redux-create-reducer

Repository files navigation

redux-create-reducer

A utility to create redux reducers from a set of handlers

About

This is a fork of @jakesidsmith/redux-create-reducer.

This utility removes the need for switch statements (which can easily result in errors), and instead allows you to create a redux reducer from a set of handler functions - no more need to handle a default case, no more boilerplate.

This library is written in TypeScript and due to how the handlers are typed, allows you to limit which actions they handle, and also define different action types for different handlers without casting their types.

Install

npm i @dabapps/redux-create-reducer -S

Usage

import { createReducer } from '@dabapps/redux-create-reducer';
import { ADD, SUBTRACT, RESET } from './action-types';

interface NumberAction {
  type: string;
  payload: number;
}

export const count = createReducer(
  {
    [ADD]: (state: number, action: NumberAction) => state + action.payload,
    [SUBTRACT]: (state: number, action: NumberAction) => state - action.payload,
    [RESET]: () => 0,
  },
  0 // Initial state (required)
);

Updating this fork from upstream

Ensure you have added a remote upstream in git e.g.

git remote add upstream [email protected]:JakeSidSmith/redux-create-reducer.git

Create a new branch (from master) and run the following to pull changes from upstream:

git fetch upstream
git pull upstream master