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

The name of the component (for use in debug mode). In release mode, this will default to "UnnamedComponent" Read more

The type of the properties passed to the component.

Create a new component with an instance of the properties. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.