Socket
Socket
Sign inDemoInstall

@georapbox/redux-create-reducer

Package Overview
Dependencies
0
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @georapbox/redux-create-reducer

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


Version published
Weekly downloads
7
increased by250%
Maintainers
1
Install size
60.1 kB
Created
Weekly downloads
 

Changelog

Source

v1.4.0

  • Add option to throw Error in development environment if an undefined action handler is encountered instead of the default behaviour which is to print a warning in console. Closes issue #6.

Readme

Source

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)

Keywords

FAQs

Last updated on 11 Dec 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc