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
  • 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:) Default implementation

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

    Default Implementation

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

    Declaration

    Swift

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

    Return Value

    A new immutable state

  • Undocumented

    Declaration

    Swift

    @inlinable
    public func callAsFunction(state: State, action: Action) -> State
  • +(_:_:) Extension method

    Compose two reducers together.

    Declaration

    Swift

    public static func + <R>(previousReducer: Self, nextReducer: R) -> CombinedReducer<State, Self, R> where R : Reducer, Self.State == R.State

    Parameters

    previousReducer

    The first reducer to be called.

    nextReducer

    The second reducer to be called.

    Return Value

    A combined reducer.