Reducer
public struct Reducer<State, Event, Environment>
A reducer is responsible for taking an event and deciding how the state should be changed and whether any effects should be executed.
-
An empty reducer. Useful for SwiftUI’s previews.
Declaration
Swift
public static var empty: Reducer<State, Event, Environment> { get }Return Value
A reducer.
-
Declaration
Swift
public typealias Reduce = (inout State, Event, Environment) -> Effect<Event>?
-
Construct a Reducer.
Declaration
Swift
public init(_ reduce: @escaping Reduce)Parameters
reduceReduce closure.
-
Transform the current reducer into one that accepts an optional state.
The reducer will only be ran when state is non-nil.
Declaration
Swift
public var optional: Reducer<State?, Event, Environment> { get }
-
Transforms or “Pulls” a local reducer into a global reducer.
The main benefit of this function is that it enables the ability to breakdown a large reducer into several smaller ones.
Declaration
Swift
public func pull<AppState, AppEvent, AppEnvironment>( state: WritableKeyPath<AppState, State>, event getEvent: @escaping (AppEvent) -> Event?, appEvent setAppEvent: @escaping (Event) -> AppEvent?, environment: @escaping (AppEnvironment) -> Environment ) -> Reducer<AppState, AppEvent, AppEnvironment>Parameters
stateA key path that sets and gets the local state from the app state.
getEventA function for optionally transforming an app event into a local event.
setAppEventA function for optionally transforming a local event that is emitted by the reducer into an app event.
environmentA function for transforming an app environment into a local environment.
Return Value
A
Reducer<AppState, AppEvent, AppEnvironment>. -
Transforms or “Pulls” a local reducer into a global reducer.
The main benefit of this function is that it enables the ability to breakdown a large reducer into several smaller ones.
Declaration
Swift
public func pull<AppState, AppEvent, AppEnvironment>( state getState: @escaping (AppState) -> State, appState setAppState: @escaping (inout AppState, State) -> Void, event getEvent: @escaping (AppEvent) -> Event?, appEvent setAppEvent: @escaping (Event) -> AppEvent?, environment: @escaping (AppEnvironment) -> Environment ) -> Reducer<AppState, AppEvent, AppEnvironment>Parameters
getStateA key path that sets and gets the local state from the app state.
setAppStateA key path that sets and gets the local state from the app state.
getEventA function for transforming an app event into a local event.
setAppEventA function for optionally transforming a local event into an app event.
environmentA function for transforming an app environment into a local environment.
Return Value
A
Reducer<AppState, AppEvent, AppEnvironment>.
-
Combines two reducers of the same
State,EventandEnvironmentinto one.The left hand reducer will be reduced first followed by the right hand reducer. Both reducer’s event streams will be merged.
Declaration
Swift
public static func <>( lhs: Reducer<State, Event, Environment>, rhs: Reducer<State, Event, Environment> ) -> Reducer<State, Event, Environment>Parameters
lhsThe left hand reducer.
rhsThe right hand reducer.
Return Value
A new reducer
Reducer Structure Reference