Actions

Plain Actions

ReduxKit only defines a minimal protocol that Action types must conform to. Action types can be enums, structs or classes. Examples:

// A simple enum with three actions
enum CountAction: Action {
    case Increment
    case Decrement
    case Set(payload: Int)
}

// The IncrementAction as a struct:
struct IncrementAction: Action {
    let payload: Int
}

// Or even a class
class IncrementActionClass: Action {
    let payload: Int
    init(payload: Int) {
        self.payload = payload
    }
}

Flux Standard Actions

Flux Standard Actions are also implemented for applications that require consistent Actions. Flux Standard Actions allows for a lot of flexibility while still maintaining a standard pattern. Example:

struct IncrementAction: StandardAction {
    let meta: Any?
    let error: Bool
    let rawPayload: Int

    init(payload: Int? = nil, meta: Any? = nil, error: Bool = false) {
        self.rawPayload = payload ?? 1
        self.meta = meta
        self.error = error
    }
}
  • Base action type all actions must conform to.

    The Action protocol is intentionally minimal to allow Action types to best fit their situation.

    If there are common properties needed on all Action types in you app, the Action protocol can be extended and each action can inherit from the new protocol instead. See FluxStandardAction for a good example of this in practice.

    See more

    Declaration

    Swift

    public protocol Action {}
  • The SimpleStandardAction contains a strongly typed rawPayload property. It is generic and expects a rawPayload of a generic type.

    The protocol automatically assigns the rawPayload to the Actions payload property. This removes the necessity of type casting whenever working with actions in a reducer.

    There’s also the StandardAction protocol, that requires the struct to have an initializer. This is required if the bindActionCreators helper is to be used.

    See more

    Declaration

    Swift

    public protocol SimpleStandardAction: FluxStandardAction
  • Optional protocol used for when actions have to be created generically

    It requires a initializer to be present

    See more

    Declaration

    Swift

    public protocol StandardAction: SimpleStandardAction
  • Helper function that helps create shorthand dispatch functions. It requires a StandardAction with a valid initializer to function.

    Declaration

    Swift

    public func bindActionCreators<Action where Action: StandardAction>(
        type: Action.Type,
        dispatch: Dispatch)
        -> (payload: Action.PayloadType?)
        -> ()

    Parameters

    type

    Type of action to bind

    dispatch

    Dispatching function (Action -> Action)

    Return Value

    Callable ActionCreator