1.4.0 • Published 5 years ago

@georapbox/redux-create-reducer v1.4.0

Weekly downloads
7
License
MIT
Repository
github
Last release
5 years ago

redux-create-reducer

Utility function to express Redux reducers as an object mapping from action types to action handlers.

npm version Travis Codecov Dependencies devDependency Status npm license

Install

$ npm install --save @georapbox/redux-create-reducer

API

createReducer(initialState, handlers , options={}) ⇒ function

Returns: function - A function that returns the next state tree, given the current state tree and the action to handle.

ParamTypeDescription
initialState*The initial state of the reducer.
handlersObject.<String, Function>A plain object mapping action types to action handlers.
options={}1ObjectA plain object for available options.

1 Available options

OptionTypeDefaultDescription
throwForUndefinedHandlersBooleanfalseIf set to true or any truthy value, it will throw Error if undefined action handler is encountered (development environment); otherwise it will just print a warning in console. It has no effect in production environment.

Usage

import createReducer from '@georapbox/redux-create-reducer';

const actionTypes = {
  ADD_TODO: 'ADD_TODO',
  TOGGLE_TODO: 'TOGGLE_TODO'
};

const initialState = [];

const handlers = {
  [actionTypes.ADD_TODO]: function addTodoHandler(state, action) {
    return [
      ...state,
      {
        id: action.id,
        text: action.text,
        completed: false
      }
    ];
  },
  [actionTypes.TOGGLE_TODO]: function toggleTodoHandler(state, ation) {
    return state.map(todo =>
      todo.id === action.id
        ? {...todo, completed: !todo.completed}
        : todo
    );
  }
};

export const todosReducer = createReducer(initialState, handlers, {
  throwForUndefinedHandlers: true
});

License

The MIT License (MIT)

1.4.0

5 years ago

1.3.1

5 years ago

1.2.0

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago