redux-combinator
Alternative reducer combinator for the redux library. Grouping action types and context names.
Install
Install with npm:
$ npm install --save redux-combinator
Usage
Task list example.
This is an example to understand how the library works. Please... In real life, do not do this!
;; // Create a Redux store holding the state of your app.// Warning: Set default state!let store = ; // This reducer creates an array of tasks. The `taskList` context. { return state} // This reducer creates and supports tasks statistics. The `taskStat` context. { // This action uses only this reducer. ifactiontype === "CLEAR_TASK_STAT" return fill: 0 add: 0 remove: 0 total: 0 // Update values for all action. let fill add remove total: prevTotal = state total = globalStatetaskListlength; return fill add remove total} // Set reducer propertiesreducerSecondreducerActionTypes = "ADD_TASK" "REMOVE_TASK" "FILL_TASK" "CLEAR_TASK_STAT";reducerSecondreducerContext = "taskStat";reducerSecondreducerDefaultValue = fill: 0 add: 0 remove: 0 total: 0; // Add reducers// The wrapper for the `rootReducer.add` method; // alternative syntax; // Dispatch actionsstore;store;store; console;
Functions
createReducer([def])
Create new reducer combinator.
Use this function when there are several store
objects in your project (createStore (reducer)
).
If you used only one root store (as Redux recommends),
you should use the rootReducer
, addReducer
, setDefault
, and getDefault
functions.
Arguments
def: any
- Default store value, global context.
Returns
Function
- new reducer function.
rootReducer(state, action)
Root reducer.
This function was created by the createReducer()
function.
Arguments
state: Object
- The type of state to be held by the store.action: Object
- The type of actions which may be dispatched.
Returns
Object
- Result mixed state.
addReducer(reducer, [actionTypes], [context], [defaultValue])
Add new reducer.
This wrapper function is for the createReducer().add
function.
Arguments
reducer: Function
- Reducer function.context: String|Number|Symbol
- Context key, use'*'
for global context (default).actionTypes: String|Number|Symbol|*[]
- Action type name or action type collection.defaultValue: any
- Any default value for used context. Ignored if added before.
The reducer function (reducerFunction) may contain (as an alternative) the following properties. In this case, it is not necessary to pass additional parameters to the function.
reducerFunction.reducerContext
- Context key.reducerFunction.reducerActionTypes
- Action type name or action type collection.reducerFunction.reducerDefaultValue
- Any default value for used context.
See the example above.
setDefault(value, [context], [override])
Sets the default value for the context state.
This wrapper function is for the createReducer().setDefault
function.
Arguments
value: any
- The context value.context: String|Number|Symbol
- Context key, use'*'
for global context.override: boolean = true
- Modify if exists. Default istrue
.
getDefault([context])
Gets the default value for the context state.
This wrapper function is for the createReducer().getDefault
function.
Arguments
context: String|Number|Symbol
- Context key, use'*'
for global context.
Returns
*
- Mixed value.
License
Copyright © 2019, GoshaV Maniako. Released under the MIT License.