@aikoven/recombine
TypeScript icon, indicating that this package has built-in type declarations

0.2.1 • Public • Published

Recombine npm version Build Status

Utilities for combining Redux reducers

Installation

npm install --save @aikoven/recombine

API

combineReducers(reducers: ReducerMapObjectWithExtraArgs): ReducerWithExtraArgs

Same as original combineReducers from Redux but returned reducer accepts extra arguments and passes them to each child reducer.

Example

Construct reducer that manages state where one branch depends on another:

import {combineReducers} from "@aikoven/recombine";

// for regular reducers `combineReducers` behaves exactly the same as original
const firstBranchReducer = combineReducers({
  something(state = ..., action) {/*...*/},
  somethingElse(state = ..., action) {/*...*/},
});

// created reducer forwards any extra args to child reducers
const secondBranchReducer = combineReducers({
  someKey(state = ..., action, firstBranch) {/*...*/},
  anotherKey(state = ..., action, firstBranch) {/*...*/},
});

const reducer = (state = {}, action) => {
  // compute first branch and pass results to the second branch
  const {firstBranch, secondBranch} = state;

  const nextFirstBranch = firstBranchReducer(firstBranch, action);
  const nextSecondBranch = secondBranchReducer(secondBranch, action, nextFirstBranch);

  if (nextFirstBranch !== firstBranch || nextSecondBranch !== secondBranch)
    return {firstBranch: nextFirstBranch, secondBranch: nextSecondBranch};

  return state;
};

dictionaryReducer(childReducer: Reducer, addKey?: Function, removeKey?: Function, initialState?: Object): Reducer

Create reducer that manages key-value dictionary state with values of same shape. Child reducer is called on every action for each dictionary value, and dictionary key is supplied to the child reducer as third argument.

Any extra arguments for resulting reducer are passed to the child reducer following dictionary key.

Parameters

  • childReducer: Reducer for dictionary values. Receives dictionary key as third argument.
  • addKey?: Function that will be called on every action to determine whether that action should result in adding a new key to the dictionary, in which case it should return the key.
  • removeKey?: Function that will be called on every action to determine whether that action should result in removing a key from the dictionary, in which case it should return the key.
  • initialState = {}: Initial state for resulting reducer.

Roadmap

  • More documentation covering rationale and use cases.
  • More tests

Readme

Keywords

Package Sidebar

Install

npm i @aikoven/recombine

Weekly Downloads

26

Version

0.2.1

License

MIT

Unpacked Size

25.2 kB

Total Files

22

Last publish

Collaborators

  • aikoven