reducer-chain
reducer-chain
helps you to chain redux
reducers with given state and action, then keep last updated state.
Getting started
Install reducer-chain
using npm:
npm install reducer-chain --save
Then using ES6
;; increment decrement;
Using ES5
var myReducers = ;var reducerChain = ; moduleexports = ;
Usage
You can rewrite this:
const reducer = { if !state state = initialState; }; ; //=> {counter: 1}
To this:
; const chain = ; const reducer = { if !state state = initialState; }; ; //=> {counter: 1}
See also
reducer-pipe
in order to pipe previously saved/returned state to next reducer in list with given action.
Examples
Compare signature
nextState
initialState
is corresponding to the state passed to the high order reducer.previousState
is corresponding to the previously returned state. Defaults toinitialState
.currentState
is corresponding to the state returned by the reducer at the current index in the list.nextState
is corresponding to the state you want to keep.initialize
will be called everytime once with the given state from the high order reducer. It must returns an iterator function for comparing previous and current state, and return the prefered one.iterator
will be called on each reducer result (passed ascurrentState
). It must compare withpreviousState
(defaults toinitialState
) and return the state you want to keep next.
Note: Please note that all reducers will be call before the iterator
.
The order of each call remain the same as given to chain
.
Available compare functions
reducer-chain
built in with 4 different compare functions available under reducerChain.compare
:
Name | Signature | Equals |
---|---|---|
withInitial (default) |
(initialState) => (previousState, currentState) => nextState |
R.equals(initialState, currentState) |
withInitialCustomEquals |
(customEquals) => (initialState) => (previousState, currentState) => nextState |
customEquals(initialState, currentState) |
withPrevious |
(initialState) => (previousState, currentState) => nextState |
R.equals(previousState, currentState) |
withPreviousCustomEquals |
(customEquals) => (initialState) => (previousState, currentState) => nextState |
customEquals(previousState, currentState) |
Compare usage
With immutable
:
// ./immutableReducerChain.js;; reducerChaincompare;
// ./index.js;;; const reducers = ; const initialState = Immutable; { return ;};
With custom compare:
;; const customCompare = currentState !== null && currentState !== initialState ? currentState : previousState; const reducers = ; const initialState = Object; { return ;};
Links
renum
is a small library to create enum using frozen objects in javascript from multiple sources.reducer-pipe
helps you to pipe given reducers with the previously saved/returned state and given action.reducer-sandbox
helps you to reuse your reducers in different place without conflict them.