Better Redux Reducers
Last updated 4 years ago by vvgomes .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install redux-definer 
SYNC missed versions from official npm registry.

Redux Definer ????

build status npm version

Redux Definer is a tiny but useful library that helps you to build reducers in a clean way. The idea is inspired by this blog post.

Getting Started

Make sure you have a recent Node version (6.0.0 should work). Then, install the package.

$ npm install redux-definer --save

Now, define some action handlers, a reducer, and a Redux store.

import { defineReducer } from "redux-definer";
import { createStore } from "redux";

const INCREMENT_COUNTER = (state, action) => state + 1;
const DECREMENT_COUNTER = (state, action) => state - 1;

const reducer = defineReducer({ INCREMENT_COUNTER, DECREMENT_COUNTER });
const store = createStore(reducer, 0);
console.log(store.getState()); // => 0

store.dispatch({ type: "INCREMENT_COUNTER" });
store.dispatch({ type: "INCREMENT_COUNTER" });
store.dispatch({ type: "DECREMENT_COUNTER" });
store.dispatch({ type: "INCREMENT_COUNTER" });
console.log(store.getState()); // => 2

store.dispatch({ type: "INVALID" });
console.log(store.getState()); // => 2


Redux reducers must conform to the following signature:

reducer :: (State, Action) -> State

There are tough no restrictions or standars in the way you can implement its behavior. Most examples in the internet - including the examples in the Redux official docs - will suggest you to define your reducers using a switch statement based on action types.

function reducer (state = [], action) {
  switch (action.type) {
    case "ADD_TODO":
      return state.concat({
        text: action.text,
        completed: false
    case "TOGGLE_TODO":
      return, index) =>
        index === action.index ?
          Object.assign({}, todo, { completed: !todo.completed }): todo
      return state;

The code in the snipet above is quite busy. The action handling logic is blended together with the reducer pattern matching logic, which makes it not very scalable or easily testable.


As you can see in Getting Started example, the defineReducer function isolates the reducer pattern matching logic and gives you the ability to define a reducer by just providing a map of action handlers. Each handler in this case is an idependent function which can be tested in isolation.

In summary, the underlying algorithm behind defineReducer can be described as:

  • It chooses a behavior by pattern-matching the action.type.
  • Then, it executes the matching behavior returning a new state.
  • In case there is no match, it returns the unchanged state.

To get more details on the way it was implemented, take a look at the blog post. To see Redux Definer in action, take a look at the Redux Todo example.


Feel free to use this code as you please.

Current Tags

  • 0.0.2                                ...           latest (4 years ago)

2 Versions

  • 0.0.2                                ...           4 years ago
  • 0.0.1                                ...           4 years ago
Maintainers (1)
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 2
Dependencies (2)
Dev Dependencies (10)
Dependents (0)

Copyright 2014 - 2016 © |