Contents

subscript(_:)

Accesses the environment value associated with a custom key.

Declaration

subscript<K>(key: K.Type) -> K.Value where K : EnvironmentKey { get set }

Overview

Create a custom environment value by declaring a new property in an extension to the environment values structure and applying the Entry() macro to the variable declaration:

extension EnvironmentValues {
    @Entry var myCustomValue: String = "Default value"
}

You use custom environment values the same way you use system-provided values, setting a value with the environment(_:_:) view modifier, and reading values with the Environment property wrapper. You can also provide a dedicated view modifier as a convenience for setting the value:

extension View {
    func myCustomValue(_ myCustomValue: String) -> some View {
        environment(\.myCustomValue, myCustomValue)
    }
}

See Also

Creating and accessing values