1.1.0 • Published 4 years ago

@dabapps/redux-create-reducer v1.1.0

Weekly downloads
97
License
MIT
Repository
github
Last release
4 years ago

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 git@github.com: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