<!--
{
  "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/TransactionKey",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI14TransactionKeyP"
  },
  "title" : "TransactionKey"
}
-->

# TransactionKey

A key for accessing values in a transaction.

```
protocol TransactionKey
```

## Overview

You can create custom transaction values by extending the [`Transaction`](/documentation/SwiftUI/Transaction)
structure with new properties.
First declare a new transaction key type and specify a value for the
required [`defaultValue`](/documentation/SwiftUI/TransactionKey/defaultValue) property:

```
private struct MyTransactionKey: TransactionKey {
    static let defaultValue = false
}
```

The Swift compiler automatically infers the associated [`Value`](/documentation/SwiftUI/TransactionKey/Value) type as the
type you specify for the default value. Then use the key to define a new
transaction value property:

```
extension Transaction {
    var myCustomValue: Bool {
        get { self[MyTransactionKey.self] }
        set { self[MyTransactionKey.self] = newValue }
    }
}
```

Clients of your transaction value never use the key directly.
Instead, they use the key path of your custom transaction value property.
To set the transaction value for a change, wrap that change in a call to
`withTransaction`:

```
withTransaction(\.myCustomValue, true) {
    isActive.toggle()
}
```

To use the value from inside `MyView` or one of its descendants, use the
[`transaction(_:)`](/documentation/SwiftUI/View/transaction(_:)) view modifier:

```
MyView()
    .transaction { transaction in
        if transaction.myCustomValue {
            transaction.animation = .default.repeatCount(3)
        }
    }
```

## Topics

### Setting a default value

[`defaultValue`](/documentation/SwiftUI/TransactionKey/defaultValue)

The default value for the transaction key.

[`Value`](/documentation/SwiftUI/TransactionKey/Value)

The associated type representing the type of the transaction key’s
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)