<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/withTransaction(_:_:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI15withTransactionyxs15WritableKeyPathCyAA0D0Vq_G_q_xyKXEtKr0_lF"
  },
  "title" : "withTransaction(_:_:_:)"
}
-->

# withTransaction(_:_:_:)

Executes a closure with the specified transaction key path and value and
returns the result.

```
@export(implementation) func withTransaction<R, V>(_ keyPath: WritableKeyPath<Transaction, V>, _ value: V, _ body: () throws -> R) rethrows -> R
```

## Parameters

`keyPath`

A key path that indicates the property of the [`Transaction`](/documentation/SwiftUI/Transaction)
structure to update.

`value`

The new value to set for the item specified by `keyPath`.

`body`

A closure to execute.

## Return Value

The result of executing the closure with the specified
transaction 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)