Struct sycamore::context::ContextProvider [−][src]
pub struct ContextProvider<G: GenericNode, T, F> { /* fields omitted */ }
Expand description
Creates a new ReactiveScope
with a context.
If a context of the given type exists already, the existing context will be shadowed within
the scope. This means that accessing the context inside the scope using use_context
will
return the new value, not the shadowed value. Using use_context
outside of this new context
scope will continue to return the old value.
Example
use sycamore::prelude::*;
use sycamore::context::{ContextProvider, ContextProviderProps, use_context};
#[derive(Clone)]
struct Counter(Signal<i32>);
#[component(CounterView<G>)]
fn counter_view() -> Template<G> {
let counter = use_context::<Counter>();
template! {
(counter.0.get())
}
}
template! {
ContextProvider(ContextProviderProps {
value: Counter(Signal::new(0)),
children: || template! {
CounterView()
}
})
}
Trait Implementations
impl<G: GenericNode, T, F> Component<G> for ContextProvider<G, T, F> where
T: 'static,
F: FnOnce() -> Template<G>,
impl<G: GenericNode, T, F> Component<G> for ContextProvider<G, T, F> where
T: 'static,
F: FnOnce() -> Template<G>,
The name of the component (for use in debug mode). In release mode, this will default to
"UnnamedComponent"
Read more
type Props = ContextProviderProps<T, F, G>
type Props = ContextProviderProps<T, F, G>
The type of the properties passed to the component.
Create a new component with an instance of the properties. Read more