Overloads
2 ways to call compose().onChange(). Each is one SwiftUI overload, picked by the arguments you pass.
compose().onChange<V>({
of: V,
initial?: boolean,
action: (oldValue: V, newValue: V) => void,
})nonisolated func onChange<V>(
of value: V,
initial: Bool = false,
_ action: @escaping (V, V) -> Void
) -> some View
where V : EquatableParameters
options.of*Voptions.initialbooleanWhether the action should be run when this view initially appears.
options.action*(oldValue: V, newValue: V) => voidA closure to run when the value changes. - term oldValue: The old value that failed the comparison check (or the initial value when requested). - term newValue: The new value that failed the comparison check.
Return value
A view that fires an action when the specified value changes.
Discussion
You can use onChange to trigger a side effect as the result of a
value changing, such as an Environment key or a Binding.
The system may call the action closure on the main actor, so avoid long-running tasks in the closure. If you need to perform such tasks, detach an asynchronous background task.
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 old and new observed values are
passed into the closure. In the following code example, PlayerView
passes both the old and new values to the model.
struct PlayerView: View {
var episode: Episode
@State private var playState: PlayState = .paused
var body: some View {
VStack {
Text(episode.title)
Text(episode.showTitle)
PlayButton(playState: $playState)
}
.onChange(of: playState) { oldState, newState in
model.playStateDidChange(from: oldState, to: newState)
}
}
}See also
More in Events and lifecycle.
- onAppearAdds an action to perform before this view appears.
- onContinueUserActivityRegisters a handler to invoke in response to a user activity that your app receives.
- onContinuousHoverAdds an action to perform when the pointer enters, moves within, and exits the view's bounds.
- onDisappearAdds an action to perform after this view disappears.
- onDragActivates this view as the source of a drag and drop operation.
- onDragSessionUpdatedSpecifies an action to perform on each update of an ongoing dragging operation activated by draggable(:) or anther drag modifiers.
- onDropDefines the destination of a drag and drop operation that handles the dropped content with a closure that you specify.
- onDropSessionUpdatedSpecifies an action to perform on each update of an ongoing drop operation activated by dropDestination(:) or other drop modifiers.
- onGeometryChangeAdds an action to be performed when a value, created from a geometry proxy, changes.
- onHingeChangeAdds an action to perform when the hinge context of the view hierarchy changes.
- onHoverAdds an action to perform when the user moves the pointer over or away from the view's frame.
- onInteractiveResizeChangeAdds an action to perform when the enclosing window is being interactively resized.