<!--
{
  "availability" : [
    "iOS: 17.0.0 -",
    "iPadOS: 17.0.0 -",
    "macCatalyst: 17.0.0 -",
    "macOS: 14.0.0 -",
    "tvOS: 17.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/Scene/onChange(of:initial:_:)-7b6vh",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI5ScenePAAE8onChange2of7initial_Qrqd___SbyyctSQRd__lF"
  },
  "title" : "onChange(of:initial:_:)"
}
-->

# onChange(of:initial:_:)

Adds an action to perform when the given value changes.

```
nonisolated func onChange<V>(of value: V, initial: Bool = false, _ action: @escaping () -> Void) -> some Scene where V : Equatable
```

## Parameters

`value`

The value to check when determining whether to run the
closure. The value must conform to the
<doc://com.apple.documentation/documentation/Swift/Equatable>
protocol.

`initial`

Whether the action should be run when this scene initially
appears.

`action`

A closure to run when the value changes.

## Return Value

A scene that triggers an action in response to a change.

## Discussion

Use this modifier to trigger a side effect when a value changes, like
the value associated with an `SwiftUI/Environment` key or a
`SwiftUI/Binding`. For example, you can clear a cache when you notice
that a scene moves to the background:

```
struct MyScene: Scene {
    @Environment(\.locale) private var locale
    @StateObject private var cache = LocalizationDataCache()

    var body: some Scene {
        WindowGroup {
            MyRootView(cache: cache)
        }
        .onChange(of: locale) {
            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: locale) {
    Task.detached(priority: .background) {
        // ...
    }
}
```

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.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)