Function yew::functional::use_context[][src]

pub fn use_context<T: Clone + PartialEq + 'static>() -> Option<T>
Expand description

Hook for consuming context values in function components. The context of the type passed as T is returned. If there is no such context in scope, None is returned. A component which calls use_context will re-render when the data of the context changes.

More information about contexts and how to define and consume them can be found on Yew Docs.

Example


#[function_component(ThemedButton)]
pub fn themed_button() -> Html {
    let theme = use_context::<Rc<ThemeContext>>().expect("no ctx found");

    html! {
        <button style={format!("background: {}; color: {}", theme.background, theme.foreground)}>
            { "Click me" }
        </button>
    }
}