Action

Action

Utility class for dealing with Redux actions. The instance of the class acts like an identifier for action. Under the hood it stil uses string identifiers and follows Flux Standard Action.

Actions can have "stages", which are basically separate actions from Redux point of view. They can be used to logically group otherwise different actions if they represent the same action, but happening in multiple stages (e.g. consider asynchronous request pattern "started" -> "success" or "error").

Constructor

new Action(baseType)

Source:
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

Stage

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.

Source:
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.

Source:
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.

Source:
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().

Source:
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.

Source:
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().

Source:
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().

Source:
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().

Source:
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().

Source:
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().

Source:
Parameters:
Name Type Description
payload

Payload to be attached to the action object.

Returns:
Type:
Object

Plain action object.

type(stageopt) → {string}

Generates type identifier for specified stage.

Source:
Parameters:
Name Type Attributes Description
stage string <optional>

Name of the stage.

Returns:
Type:
string

Type for the action in specified stage.