Methods
conditionalReducer(conditionalFn, thenReducerFn, elseReducerFnopt, initialStateopt) → {reducerFn}
- Source:
Create a higher-order reducer that conditionally executes one of the supplied reducerFns, based on the conditionalFn() return directive.
The Dev Guide discusses conditionalReducer() in more detail (see Conditional Reduction), and additional examples can be found in Joining Reducers and A Most Excellent Example.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
conditionalFn |
conditionalReducerCB | a callback function whose return value determines which reducerFn is executed ... truthy: thenReducerFn(), falsy: elseReducerFn(). |
||
thenReducerFn |
reducerFn | the "wrapped" reducer invoked when conditionalFn returns truthy. |
||
elseReducerFn |
reducerFn |
<optional> |
identity
|
the optional "wrapped" reducer invoked when conditionalFn returns falsy. DEFAULT: identity function |
initialState |
InitialState |
<optional> |
the optional fall-back state value used during the state initialization boot-strap process. |
Returns:
a newly created reducer function (described above).
- Type
- reducerFn
joinReducers(…reducerFns, initialStateopt) → {reducerFn}
- Source:
Create a higher-order reducer by combining two or more reducers, logically executing each in sequence (in essence combining their functionality into one). This is useful when combining various reducer types into one logical construct.
Please Note: Because each reducer is able to build on what has been accomplished by a prior reducer, joinReducers cumulatively passes the state parameter that was returned from any prior reducer (in the chain of reducers to execute). In essence this is an accumulative process. While this does NOT relax the immutable constraint of the reducer's state parameter, it is possible for a down-stream reducer to receive a state parameter that is a different instance from the start of the reduction process (because an up-stream reducer needed to alter it in some way).
The Dev Guide discusses joinReducers() in more detail (see Joining Reducers), and additional examples can be found in A Most Excellent Example.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
reducerFns |
reducerFn |
<repeatable> |
two or more reducer functions to join together. |
initialState |
InitialState |
<optional> |
the optional fall-back state value used during the state initialization boot-strap process. |
Returns:
a newly created reducer function (described above).
- Type
- reducerFn
reducerHash(actionHandlers, initialStateopt) → {reducerFn}
- Source:
Create a higher-order reducer by combining a set of sub-reducer functions that are indexed by the standard action.type. When no action.type is acted on, the original state is merely passed-through (using the identity function).
This is one of the more prevalent composition reducers, and provides an alternative to the switch statement (commonly used to provide this control mechanism).
The Dev Guide discusses reducerHash() in more detail (see Basics), and additional examples can be found in Joining Reducers and A Most Excellent Example.
NOTE: Because reducerHash is so central to the rudimentary aspect of reduction, it is a common practice to extend it, promoting a centralized reducer-based logging capability, with an ability to correlate logging levels to state changes (providing a means to filter logs at a high level with minimal output).
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
actionHandlers |
ActionReducerHash | a hash of reducer functions, indexed by the standard redux action.type. |
|
initialState |
InitialState |
<optional> |
the optional fall-back state value used during the state initialization boot-strap process. |
Returns:
a newly created reducer function (described above).
- Type
- reducerFn
Type Definitions
Action
- Source:
Properties:
Name | Type | Description |
---|---|---|
type |
string | Symbol | The action type. |
whatever |
* | Additional app-specific payload (as needed). |
A standard redux Action object that drives the reduction process.
Type:
- Object
ActionReducerHash
- Source:
Properties:
Name | Type | Description |
---|---|---|
actionType1 |
reducerFn | The reducer function servicing: 'actionType1'. |
actionType2 |
reducerFn | The reducer function servicing: 'actionType2'. |
...more |
reducerFn | ...etc. |
A hash of reducer functions, indexed by the standard redux action.type.
Type:
- Object
conditionalReducerCB(state, action, originalReducerState) → {truthy}
- Source:
A callback function (used in conditionalReducer
) whose
return value determines which reducerFn is executed.
Parameters:
Name | Type | Description |
---|---|---|
state |
* | The current immutable state that is the reduction target. |
action |
Action | The standard redux Action object that drives the reduction process. |
originalReducerState |
* | The immutable state at the time of the start of the reduction process. This is useful in determining whether state has changed within a
series of reductions Further information can be found in the originalReducerState discussion of the Dev Guide. |
Returns:
A truthy value indicating which reducerFn is executed ... truthy: thenReducerFn(), falsy: elseReducerFn().
- Type
- truthy
InitialState
- Source:
All astx-redux-util reducer creators, expose an initialState
parameter which optionally provides a fall-back state value to use
during the state initialization boot-strap process.
In general, redux expects your state to have concrete values
(i.e. something other than undefined
). This means that the
reduction entry point to each state element should define a
default. Keeping this in mind, the initialState
parameter is
optional, because some reducers are "by design" (when combined in a
composition) intended to be mid-stream processors (i.e. NOT the
reduction entry point).
Type:
- *
reducerFn(state, action) → {*}
- Source:
A standard redux reducer function that is responsible for state changes.
Parameters:
Name | Type | Description |
---|---|---|
state |
* | The current immutable state that is the reduction target. |
action |
Action | The standard redux action which drives the reduction process. |
Returns:
The resulting state after reduction.
- Type
- *