Context

Context passes serializable values down the component tree without prop drilling.

LIVE

provide_context

Context locale: es

Define a context

#[data]
struct Locale {
    lang: String,
}

static LOCALE: ContextId<Locale> = ContextId::new();

Provide and consume

#[component]
fn App() {
    provide_context(&LOCALE, Locale { lang: "en".into() });
    view! { <Page /> }
}

#[component]
fn Page() {
    let locale = use_context(&LOCALE);
    view! {
        <p>"Language: " {locale.lang.clone()}</p>
    }
}

Resumability

Context values are serialized in the resumability payload. Descendants can read them on the client after resume without re-fetching from the server.

Theme helper

For theming, see Theme cookbook which wraps context with provide_theme / use_theme.