---
title: "onPreferenceChange(_:perform:)"
framework: swiftui
role: symbol
role_heading: Instance Method
path: "swiftui/view/onpreferencechange(_:perform:)"
---

# onPreferenceChange(_:perform:)

Adds an action to perform when the specified preference key’s value changes.

## Declaration

```swift
nonisolated func onPreferenceChange<K>(_ key: K.Type = K.self, perform action: @escaping (K.Value) -> Void) -> some View where K : PreferenceKey, K.Value : Equatable

```

## Parameters

- `key`: The key to monitor for value changes.
- `action`: The action to perform when the value for key changes. The action closure passes the new value as its parameter.

## Return Value

Return Value A view that triggers action when the value for key changes.
