<!--
{
  "documentType" : "article",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View-State",
  "metadataVersion" : "0.1.0",
  "role" : "collectionGroup",
  "title" : "State modifiers"
}
-->

# State modifiers

Access storage and provide child views with configuration data.

## Overview

SwiftUI provides tools for managing data in your app. For example,
you can store values and objects in an environment that’s
shared among the views in a view hierarchy. Any view that shares the
environment — typically all the descendant views of the view that
stores the item — can then access the stored item.

For more information about the types that SwiftUI provides to help manage
data in your app, see [Model data](/documentation/SwiftUI/Model-data).

## Topics

### Identity

[`func tag<V>(V, includeOptional: Bool) -> some View`](/documentation/SwiftUI/View/tag(_:includeOptional:))

Sets the unique tag value of this view.

[`func id<ID>(ID) -> some View`](/documentation/SwiftUI/View/id(_:))

Binds a view’s identity to the given proxy value.

[`func equatable() -> EquatableView<Self>`](/documentation/SwiftUI/View/equatable())

Prevents the view from updating its child view when its new value is the
same as its old value.

### Environment values

[`func environment<T>(T?) -> some View`](/documentation/SwiftUI/View/environment(_:))

Places an observable object in the view’s environment.

[`func environment<V>(WritableKeyPath<EnvironmentValues, V>, V) -> some View`](/documentation/SwiftUI/View/environment(_:_:))

Sets the environment value of the specified key path to the given value.

[`func environmentObject<T>(T) -> some View`](/documentation/SwiftUI/View/environmentObject(_:))

Supplies an observable object to a view’s hierarchy.

[`func transformEnvironment<V>(WritableKeyPath<EnvironmentValues, V>, transform: (inout V) -> Void) -> some View`](/documentation/SwiftUI/View/transformEnvironment(_:transform:))

Transforms the environment value of the specified key path with the
given function.

### Preferences

[`func preference<K>(key: K.Type, value: K.Value) -> some View`](/documentation/SwiftUI/View/preference(key:value:))

Sets a value for the given preference.

[`func transformPreference<K>(K.Type, (inout K.Value) -> Void) -> some View`](/documentation/SwiftUI/View/transformPreference(_:_:))

Applies a transformation to a preference value.

[`func anchorPreference<A, K>(key: K.Type, value: Anchor<A>.Source, transform: (Anchor<A>) -> K.Value) -> some View`](/documentation/SwiftUI/View/anchorPreference(key:value:transform:))

Sets a value for the specified preference key, the value is a
function of a geometry value tied to the current coordinate
space, allowing readers of the value to convert the geometry to
their local coordinates.

[`func transformAnchorPreference<A, K>(key: K.Type, value: Anchor<A>.Source, transform: (inout K.Value, Anchor<A>) -> Void) -> some View`](/documentation/SwiftUI/View/transformAnchorPreference(key:value:transform:))

Sets a value for the specified preference key, the value is a
function of the key’s current value and a geometry value tied
to the current coordinate space, allowing readers of the value
to convert the geometry to their local coordinates.

[`func onPreferenceChange<K>(K.Type, perform: (K.Value) -> Void) -> some View`](/documentation/SwiftUI/View/onPreferenceChange(_:perform:))

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

[`func backgroundPreferenceValue<Key, T>(Key.Type, (Key.Value) -> T) -> some View`](/documentation/SwiftUI/View/backgroundPreferenceValue(_:_:))

Reads the specified preference value from the view, using it to
produce a second view that is applied as the background of the
original view.

[`func backgroundPreferenceValue<K, V>(K.Type, alignment: Alignment, (K.Value) -> V) -> some View`](/documentation/SwiftUI/View/backgroundPreferenceValue(_:alignment:_:))

Reads the specified preference value from the view, using it to
produce a second view that is applied as the background of the
original view.

[`func overlayPreferenceValue<Key, T>(Key.Type, (Key.Value) -> T) -> some View`](/documentation/SwiftUI/View/overlayPreferenceValue(_:_:))

Reads the specified preference value from the view, using it to
produce a second view that is applied as an overlay to the
original view.

[`func overlayPreferenceValue<K, V>(K.Type, alignment: Alignment, (K.Value) -> V) -> some View`](/documentation/SwiftUI/View/overlayPreferenceValue(_:alignment:_:))

Reads the specified preference value from the view, using it to
produce a second view that is applied as an overlay to the
original view.

### Default storage

[`func defaultAppStorage(UserDefaults) -> some View`](/documentation/SwiftUI/View/defaultAppStorage(_:))

The default store used by `AppStorage` contained within the view.

### Configuring a model

[`func modelContext(ModelContext) -> some View`](/documentation/SwiftUI/View/modelContext(_:))

Sets the model context in this view’s environment.

[`func modelContainer(ModelContainer) -> some View`](/documentation/SwiftUI/View/modelContainer(_:))

Sets the model container and associated model context in this
view’s environment.

[`func modelContainer(for:inMemory:isAutosaveEnabled:isUndoEnabled:onSetup:)`](/documentation/SwiftUI/View/modelContainer(for:inMemory:isAutosaveEnabled:isUndoEnabled:onSetup:))

Sets the model container in this view for storing the provided
model type, creating a new container if necessary, and also sets a model
context for that container in this view’s environment.



---

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)