CombinedReducer
public final class CombinedReducer: Reducer
Reducer that represents a combination of two reducers
This reducers is not to be implemented by hand. You combine reducer by adding them with the +
operator.
let combinedReducers = Reducer1() + Reducer2() + Reducer3()
Example
Combine reducers
If your application has two screens, you can model that with two states, for example TodoList
and Settings
states.
We can then create two reducers for each of these states.
let todoReducer = BlockReducer(state: TodoList.self) { state, action in
guard let newState = state as? Int else { return state }
if action is SomeAction {
return newState + 1
}
return newState
}
let settingsReducer = BlockReducer(state: Settings.self) { state, action in
guard let newState = state as? Int else { return state }
if action is SomeAction {
return newState + 1
}
return newState
}
We can then combine these 2 reducers with:
let store = Suas.createStore(
reducer: todoReducer + settingsReducer
)
todoReducer
will handle the TodoList
key of state and settingsReducer
will handle the Settings
portoon of the full Store state
Combine reducers with custom state key
If a store has a state with two keys key1
and key2
. We can register a reducer for each one of these keys.
We create two reducers with stateKey
s of key1
and key2
let myReducer1 = BlockReducer(state: 1, key: "key1") { state, action in
guard let newState = state as? Int else { return state }
if action is SomeAction {
return newState + 1
}
return newState
}
let myReducer2 = BlockReducer(state: 1, key: "key2") { state, action in
guard let newState = state as? Int else { return state }
if action is SomeAction {
return newState + 1
}
return newState
}
We can then combine these 2 reducers with:
let store = Suas.createStore(
reducer: myReducer1 + myReducer2
)
myReducer1
will handle the key1
key of state and myReducer2
will handle the key2
key of state
-
Declaration
Swift
public var initialState: Any
-
Declaration
Swift
public func reduce(state: Any, action: Action) -> Any?
Parameters
state
action