Reducer
public protocol Reducer
Protocol that represents a reducer
Example
struct MyReducer: Reducer {
var initialState: Int = 1
func reduce(state: Int, action: Action) -> Int? {
// Cast the action to a specific action
if let action = action as? SomeAction {
// state changed, listeners will be notified
return state + 1
}
// returning nil means the state did not change and listeners wont be notified
return nil
}
}
Note: Returning nil
from reduce
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
var initialState: StateType
-
stateKey
Default implementation(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.Default Implementation
Undocumented
Declaration
Swift
var stateKey: StateKey
-
Generates a new state from the old state and an action
Note: Returning
nil
fromreduce
signifies that the state did not change which will not inform the listenersDeclaration
Swift
func reduce(state: StateType, action: Action) -> StateType?
Parameters
state
the current state for this reducer type
action
the action dispatched to the store
Return Value
the new state