BlockReducer
public final class BlockReducer<StateType>: Reducer
Create a reducer inline with a block
Example
Create a reducer without defining a class/struct that implements Reducer
let myReducer = BlockReducer(state: 1) { state, action in
guard let newState = state as? Int else { return state }
// cast the action to a specific type
if action is SomeAction {
return newState + 1
}
return newState
}
Note: Returning nil from BlockReducer signifies that the state did not change which will not inform the listeners
-
Inital state value for this particular reducer This initial state will be used to populate the Store state (which represents the app state)
Declaration
Swift
public let initialState: StateType -
(Optional) The state key for this reducer. If not implemented (or set) the type of
initialStatewill be used as a key.It is recommended to not set the
stateKeyas it will use the type name of theinitialStateinstead.Declaration
Swift
public let stateKey: String -
Create a reducer with a state and a reduce function
Declaration
Swift
public convenience init(initialState: StateType, reduce: @escaping ReducerFunction<StateType>)Parameters
statethe initial state of the reducer. This initial state will be used to populate the Store state (which represents the app state)
reducethe reduce function. A block that receives the current state and dispatched action and return a new state (or nil if the action did not change the state)
-
Create a reducer with a state, a state key, and a reduce function
Declaration
Swift
public init(initialState: StateType, stateKey: StateKey, reduce: @escaping ReducerFunction<StateType>)Parameters
initialStatethe initial state of the reducer. This initial state will be used to populate the Store state (which represents the app state)
stateKeyThe state key for this reducer. If not implemented (or set) the type of
initialStatewill be used as a key.reducethe reduce function. A block that receives the current state and dispatched action and return a new state (or nil if the action did not change the state)
-
Declaration
Swift
public func reduce(state: StateType, action: Action) -> StateType?Parameters
stateaction
View on GitHub
BlockReducer Class Reference