1.0.5 • Published 6 years ago

redux-reducer-composers v1.0.5

Weekly downloads
4
License
ISC
Repository
github
Last release
6 years ago

Functions for composing reducers. These functions are then composable with other reducers, for instance with Redux's combineReducers.

Install

npm install redux-reducer-composers --save-dev

Compositors

Compose reducers

composeReducers(reducers: array, defaultState: object);

When given a list of reducers, it will return a new reducer that calls each provided reducer in sequence until a new state is returned.

Create reducer from map

createReducerFromMap(map: object, defaultState: object);

When given a map of reducers, with action strings as keys, it will return a new reducer that calls the appropriate reducer when that action is received.

This approach should prevent the long string of switch statements common to reducers.

Example

import { createStore } from 'redux';
import { createReducerFromMap } from 'redux-reducer-composers';

const ACTION_FOO = 'foo';
const ACTION_BAR = 'bar';

const reducerMap = {
  [ACTION_FOO]: (state, action) => { ...newState }
};

const store = createStore(createReducerFromMap(reducerMap));

store.dispatch({ type: ACTION_FOO }); // State is updated
store.dispatch({ type: ACTION_BAR }); // State is not updated