Skip to main content

environment_reader

Function environment_reader 

Source
pub fn environment_reader(
    builder: impl Fn(EnvironmentValues) -> ViewHandle + 'static,
) -> ViewHandle
Expand description

A view whose content is a function of the environment.

This is what @Environment is for, without needing to be a view type. A Swift view declares stored Environment<V> properties and SwiftUI finds them by reflecting over the type’s field descriptors, populates them, and then calls body — machinery that only exists for types the compiler emitted. EnvironmentReader takes the closure instead, so a view built out of values can still depend on the environment:

ⓘ
environment_reader(|env| {
    if env.voice_over_enabled() { button() } else { empty() }
})

The closure is called during layout, every time the environment changes, so it is leaked rather than borrowed from the frame that built it.