ContextProvider

Struct ContextProvider 

Source
pub struct ContextProvider;
Expand description

ContextProvider is a component that provides a context to its children.

Once a context is provided, it can be accessed by the children using the UseContext hook.

§Example

struct NumberOfTheDay(i32);

#[component]
fn MyContextConsumer(hooks: Hooks) -> impl Into<AnyElement<'static>> {
    let number = hooks.use_context::<NumberOfTheDay>();

    element! {
        View(border_style: BorderStyle::Round, border_color: Color::Cyan) {
            Text(content: "The number of the day is... ")
            Text(color: Color::Green, weight: Weight::Bold, content: number.0.to_string())
            Text(content: "!")
        }
    }
}

fn main() {
    element! {
        ContextProvider(value: Context::owned(NumberOfTheDay(42))) {
            MyContextConsumer
        }
    }
    .print();
}

Trait Implementations§

Source§

impl Component for ContextProvider

Source§

type Props<'a> = ContextProviderProps<'a>

The type of properties that the component accepts.
Source§

fn new(_props: &Self::Props<'_>) -> Self

Creates a new instance of the component from a set of properties.
Source§

fn update( &mut self, props: &mut Self::Props<'_>, _hooks: Hooks<'_, '_>, updater: &mut ComponentUpdater<'_, '_, '_>, )

Invoked whenever the properties of the component or layout may have changed.
Source§

fn draw(&mut self, _drawer: &mut ComponentDrawer<'_>)

Invoked to draw the component.
Source§

fn poll_change(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<()>

Invoked to determine whether a change has occurred that would require the component to be updated and redrawn.
Source§

impl Default for ContextProvider

Source§

fn default() -> ContextProvider

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Any for T
where T: Any,

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<C> ElementType for C
where C: Component,

Source§

type Props<'a> = <C as Component>::Props<'a>

The type of the properties that can be passed to the element.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.