Reducer

public protocol Reducer

Performs an action on a given state and returns a whole new version.

A store is given a single root Reducer. As it’s sent actions, it runs the reducer to update the application’s state. The reducer can have subreducers to separate code out into modular parts.

For a reducer’s own state and actions, implement the reduce(state:action:). For subreducers, implement the reduceNext(state:action:) method.

  • The type of state that the Reducer is able to mutate.

    Declaration

    Swift

    associatedtype State : Decodable, Encodable
  • The supported actions of a reducer.

    Declaration

    Swift

    associatedtype ReducerAction
  • reduce(state:action:) Default implementation

    Operates on the state with the reducer’s own actions, returning a fresh new copy of the state.

    Default Implementation

    Default implementation. Returns the state without modifying it.

    Declaration

    Swift

    func reduce(state: State, action: ReducerAction) -> State

    Return Value

    A new immutable state.

  • reduceNext(state:action:) Default implementation

    Delegates an action to a subreducer.

    Default Implementation

    Default implementation. Returns the state without modifying it.

    Declaration

    Swift

    func reduceNext(state: State, action: Action) -> State

    Return Value

    A new immutable state.

  • reduceAny(state:action:) Extension method

    Send any kind of action to a reducer. The recuder will determine what it can do with the action.

    Declaration

    Swift

    public func reduceAny(state: State, action: Action) -> State

    Return Value

    A new immutable state