Constructor
new Action(baseType)
Parameters:
Name | Type | Description |
---|---|---|
baseType |
string
|
Base type identifier for the action. |
Example
const todoAdded = new Action("todoAdded")
// Dispatch
dispatch(todoAdded.action({ text: "Todo text" }))
// Reducer
const reducer = Action.createReducer(
Action.initial({ todos: [] }), // default initial value is { }
todoAdded.on((state, item) => ({ todos: [...state.todos, item] }))
)
Namespaces
Methods
(static) createReducer(subKeyopt, …args) → {function}
Combines all of the passed handlers to form a single reducer. Handlers can be a wrapped handlers from Action.on() calls as well as regular reducer functions.
The default initial value for reducer is { }
(empty object). It can be overridden with
Action.initial().
If the first argument passed is a string, the resulting reducer will have all of the attached handlers operating on the sub key of the passed state rather than on the state itself.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
subKey |
string
|
<optional> |
Sub key for handlers to operate on. |
args |
function
|
<repeatable> |
Handler functions to be combined into the single reducer. |
Returns:
- Type:
-
function
Redux-compatible reducer function.
(static) initial(valueOrFunc)
Pass the result of the call to createReducer() to define the initial value of the resulting reducer.
Parameters:
Name | Type | Description |
---|---|---|
valueOrFunc |
If function is passed it will be called when reducer needs initial value. Otherwise provided value is used as is by the reducer. |
Example
Action.createReducer(
Action.initial({ foo: 'bar' })
)
action(stageopt, payload) → {Object}
Creates plain action object which can be passed to dispatch()
function.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
stage |
string
|
<optional> |
To use when creating the action object. |
payload |
Payload to be attached to the action object. |
Returns:
- Type:
-
Object
Plain action object.
error(payload) → {Object}
Convenience function. Create plain action object with ERROR
stage.
See action().
Parameters:
Name | Type | Description |
---|---|---|
payload |
Payload to be attached to the action object. |
Returns:
- Type:
-
Object
Plain action object.
on(stageopt, handler) → {function}
Wraps the provided handler function into a condition check, so the function is only called if it receives the action with type equal to one of this action. Stage is taken into account if specified. Reducer function will receive only payload part of the action.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
stage |
stage
|
<optional> |
Process actions of specified stage. |
handler |
function
|
Handler function. |
Returns:
- Type:
-
function
Wrapper handler function.
onError(handler) → {function}
Convenience function. Wraps handler for processing ERROR
stage.
See on().
Parameters:
Name | Type | Description |
---|---|---|
handler |
function
|
Handler function. |
Returns:
- Type:
-
function
Wrapper handler function.
onStarted(handler) → {function}
Convenience function. Wraps handler for processing STARTED
stage.
See on().
Parameters:
Name | Type | Description |
---|---|---|
handler |
function
|
Handler function. |
Returns:
- Type:
-
function
Wrapper handler function.
onSuccess(handler) → {function}
Convenience function. Wraps handler for processing SUCCESS
stage.
See on().
Parameters:
Name | Type | Description |
---|---|---|
handler |
function
|
Handler function. |
Returns:
- Type:
-
function
Wrapper handler function.
started(payload) → {Object}
Convenience function. Create plain action object with STARTED
stage.
See action().
Parameters:
Name | Type | Description |
---|---|---|
payload |
Payload to be attached to the action object. |
Returns:
- Type:
-
Object
Plain action object.
success(payload) → {Object}
Convenience function. Create plain action object with SUCCESS
stage.
See action().
Parameters:
Name | Type | Description |
---|---|---|
payload |
Payload to be attached to the action object. |
Returns:
- Type:
-
Object
Plain action object.