<!--
{
  "availability" : [
    "iOS: 17.0.0 -",
    "iPadOS: 17.0.0 -",
    "macOS: 14.0.0 -",
    "tvOS: 17.0.0 -",
    "watchOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/modelContext(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI",
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewP01_a5Data_aB0E12modelContextyQr0aD005ModelF0CF"
  },
  "title" : "modelContext(_:)"
}
-->

# modelContext(_:)

Sets the model context in this view’s environment.

```
@MainActor @preconcurrency func modelContext(_ modelContext: ModelContext) -> some View
```

## Parameters

`modelContext`

The model context to set in this view’s environment.

## Discussion

In this example, the `RecipesList` view sets a model context to use
for all of its content:

```
@Model class Recipe { ... }
...
RecipesList()
    .modelContext(myContext)
```

The environment’s [`modelContext`](/documentation/SwiftUI/EnvironmentValues/modelContext) property will be
assigned `myContext`. All implicit model context operations in this
view, such as `Query` properties, will use the environment’s context.

---

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)