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
initialState
will be used as a key.It is recommended to not set the
stateKey
as it will use the type name of theinitialState
instead.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
state
the initial state of the reducer. This initial state will be used to populate the Store state (which represents the app state)
reduce
the 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
initialState
the initial state of the reducer. This initial state will be used to populate the Store state (which represents the app state)
stateKey
The state key for this reducer. If not implemented (or set) the type of
initialState
will be used as a key.reduce
the 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
state
action