create-reducer-object
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

create-reducer-object

This is a very simple helper library. Traditionally, according to the Redux documentation, reducers are written like this:

function myFirstReducer(state, action) {
  switch (action.type) {
    case 'TODO_ADDED':
      return todoAdded(state, action);
  }

  return state;
}

This results in long, unwieldy switch statements when you have many different actions.

You can write the equivalent of the above, using this library, as follows:

import { createReducerObject } from 'create-reducer-object';

const initialState = {
  todos: [],
};

const myFirstReducer = createReducerObject({
  TODO_ADDED: todoAdded
}, initialState);

Typescript compatibility

The module is written in and fully compatible with Typescript. You can define your state like so:

import { createReducerObject } from 'create-reducer-object';

type State = {
  todos: Todo[];
};

const initialState: State = {
  todos: [],
};

const myFirstReducer = createReducerObject<State>({
  TODO_ADDED: todoAdded,
}, initialState);

/create-reducer-object/

    Package Sidebar

    Install

    npm i create-reducer-object

    Weekly Downloads

    0

    Version

    2.0.1

    License

    MIT

    Unpacked Size

    6.99 kB

    Total Files

    15

    Last publish

    Collaborators

    • felamaslen