Function freya::prelude::use_context_provider
pub fn use_context_provider<T>(f: impl FnOnce() -> T) -> Twhere
T: 'static + Clone,
Expand description
Provide some context via the tree and return a reference to it
Once the context has been provided, it is immutable. Mutations should be done via interior mutability. Context can be read by any child components of the context provider, and is a solution to prop drilling, using a context provider with a Signal inside is a good way to provide global/shared state in your app:
fn app() -> Element {
use_context_provider(|| Signal::new(0));
rsx! { Child {} }
}
// This component does read from the signal, so when the signal changes it will rerun
#[component]
fn Child() -> Element {
let mut signal: Signal<i32> = use_context();
rsx! {
button { onclick: move |_| signal += 1, "increment context" }
p {"{signal}"}
}
}