reducer
Official Reducers for fluxette, and compatible with Redux.
What's a Reducer?
In case you didn't get here from fluxette or Redux, a Reducer is simply a function that takes an aggregate, which we refer to as a state, and a transaction, which we refer to as an action, which it combines, or reduces, to produce a new state. Thus, they all have the signature of (State, Action) => State)
. Reducers are very useful for state management, a large element of the Flux Architecture.
A fundamental property of Reducers is that they are composable, which allows for orthogonal code, separation of concerns, and easy scaling. This means that one reducer can hold other reducers, which hold even more reducers.
Install
npm install --save reducer
Reducer Creators
Shape(shape) Creates a reducer that holds more reducers on each of its properties. The property names reflect how they will appear on the state.
;; let store = ; // { stock: stock(), cart: cart() }
Leaf(initial, reducers)
Creates a reducer that applies its reducers to a state respective to the type of the action provided (action.type
), using initial
as the initial state.
; let dataReducer = ;
Filter(types, reducer) Creates a reducer that proxies its action to its reducers only if the action's type matches one of its types.
; let userReducer = ;
History() Creates a reducer that keeps track of the actions that have been dispatched.
;; let reducer = ) let state;state = state = state = statehistory// [{ type: TYPE_A, data: 'a' }, { type: TYPE_B, data: 'b' }, { type: TYPE_C, data: 'c' }]
Hydrate(reducer)
Creates a reducer that wraps reducer
, and replaces the state with action.state
if the action is of type Hydrate:type
.
; let reducer = ; let state;state = ; state// { a: 5, b: 6 }
SideEffect(reducer, effect)
Creates a reducer that wraps reducer
, and calls effect
as if it were a Reducer, but ignores its return value.
; let loggingReducer = ; // effectively the same as reducer(state, action), but also logs actions