Function dioxus_hooks::use_shared_state_provider
source · pub fn use_shared_state_provider<T: 'static>(
cx: &ScopeState,
f: impl FnOnce() -> T
)
Expand description
Provide some state for components down the hierarchy to consume without having to drill props. See use_shared_state
to consume the state
Example
#[derive(Clone, Copy)]
enum Theme {
Light,
Dark,
}
// Provider
fn Parent<'a>(cx: Scope<'a>) -> Element<'a> {
use_shared_state_provider(cx, || Theme::Dark);
let theme = use_shared_state::<Theme>(cx).unwrap();
render! {
button{
onclick: move |_| {
let current_theme = *theme.read();
*theme.write() = match current_theme {
Theme::Dark => Theme::Light,
Theme::Light => Theme::Dark,
};
},
"Change theme"
}
// Children components that consume the state...
}
}