onChange(of:removeDuplicates:_:)
Adds a reducer to run when this reducer changes the given value in state.
This declaration is deprecated: Use 'onChange(of:)' with and equatable value, instead.
func onChange<V, R>(of toValue: @escaping (State) -> V, removeDuplicates isDuplicate: @escaping (V, V) -> Bool, @ReducerBuilder<Self.State, Self.Action> _ reducer: @escaping (_ oldValue: V, _ newValue: V) -> R) -> _OnChangeReducer<Self, V, R> where R : Reducer, Self.Action == R.Action, Self.State == R.State
Parameters
- toValue
A closure that returns a value from the given state.
- isDuplicate
A closure to evaluate whether two elements are equivalent, for purposes of filtering. Return
true
from this closure to indicate that the second element is a duplicate of the first.- reducer
A reducer builder closure to run when the value changes.
oldValue: The old value that failed the comparison check.
newValue: The new value that failed the comparison check.
Returns
A reducer that performs the logic when the state changes.
Use this operator to trigger additional logic when a value changes, like when a BindingReducer
makes a deeper change to a struct held in BindingState
.
@Reducer
struct Settings {
struct State {
@BindingState var userSettings: UserSettings
// ...
}
enum Action: BindableAction {
case binding(BindingAction<State>)
// ...
}
var body: some Reducer<State, Action> {
BindingReducer()
.onChange(
of: { ($0.userSettings.isHapticFeedbackEnabled, $0.userSettings.isPushEnabled) },
removeDuplicates: ==
) { oldValue, newValue in
Reduce { state, action in
.run { send in
// Persist new value...
}
}
}
}
}
When the value changes, the new version of the closure will be called, so any captured values will have their values from the time that the observed value has its new value. The system passes the old and new observed values into the closure.