Skip to main content

with_environment

Function with_environment 

Source
pub fn with_environment<T>(
    value: T,
    content: impl FnOnce() -> AnyPiece + 'static,
) -> AnyPiece
where T: Clone + 'static,
Expand description

Provide an ambient value T to content and its ENTIRE descendant subtree (the SwiftUI @Environment/.environment(_) analog, layered over day-reactive’s scope context). content — and any piece built within it — reads it back with environment. A thin, non-reactive wrapper: T is a snapshot captured here; for a value that must react, provide a Signal<T> (or a Memo<T>) and read it reactively inside the subtree.

#[derive(Clone)] struct Theme { accent: Color }
with_environment(Theme { accent: BLUE }, || my_screen())
// deep inside my_screen():  let accent = environment::<Theme>().unwrap().accent;
Day API ↩ Guides· daybrite.dev