---
title: "onChange(of:perform:)"
framework: swiftui
role: symbol
role_heading: Instance Method
path: "swiftui/view/onchange(of:perform:)"
---

# onChange(of:perform:)

Adds an action to perform when the given value changes.

## Declaration

```swift
nonisolated func onChange<V>(of value: V, perform action: @escaping (V) -> Void) -> some View where V : Equatable

```

## Discussion

Discussion Use this modifier to trigger a side effect when a value changes, like the value associated with an Environment value or a Binding. For example, you can clear a cache when you notice that a scene moves to the background: struct MyScene: Scene {     @Environment(\.scenePhase) private var scenePhase     @StateObject private var cache = DataCache()

var body: some Scene {         WindowGroup {             MyRootView()         }         .onChange(of: scenePhase) { newScenePhase in             if newScenePhase == .background {                 cache.empty()             }         }     } } 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: .onChange(of: scenePhase) { newScenePhase in     if newScenePhase == .background {         Task.detached(priority: .background) {             // ...         }     } } The system passes the new value into the closure. If you need the old value, capture it in the closure.

## See Also

### Input and events modifiers

- [dropDestination(for:action:isTargeted:)](swiftui/view/dropdestination(for:action:istargeted:).md)
- [onTapGesture(count:coordinateSpace:perform:)](swiftui/view/ontapgesture(count:coordinatespace:perform:)-36x9h.md)
- [onLongPressGesture(minimumDuration:maximumDistance:pressing:perform:)](swiftui/view/onlongpressgesture(minimumduration:maximumdistance:pressing:perform:).md)
- [onLongPressGesture(minimumDuration:pressing:perform:)](swiftui/view/onlongpressgesture(minimumduration:pressing:perform:).md)
- [onPasteCommand(of:perform:)](swiftui/view/onpastecommand(of:perform:)-4f78f.md)
- [onPasteCommand(of:validator:perform:)](swiftui/view/onpastecommand(of:validator:perform:)-964k1.md)
- [onDrop(of:delegate:)](swiftui/view/ondrop(of:delegate:)-2vr9o.md)
- [onDrop(of:isTargeted:perform:)](swiftui/view/ondrop(of:istargeted:perform:).md)
- [focusable(_:onFocusChange:)](swiftui/view/focusable(_:onfocuschange:).md)
- [onContinuousHover(coordinateSpace:perform:)](swiftui/view/oncontinuoushover(coordinatespace:perform:)-8gyrl.md)
