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
impl Component for ContextProvider
Source§type Props<'a> = ContextProviderProps<'a>
type Props<'a> = ContextProviderProps<'a>
The type of properties that the component accepts.
Source§fn new(_props: &Self::Props<'_>) -> Self
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<'_, '_, '_>,
)
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<'_>)
fn draw(&mut self, _drawer: &mut ComponentDrawer<'_>)
Invoked to draw the component.
Source§impl Default for ContextProvider
impl Default for ContextProvider
Source§fn default() -> ContextProvider
fn default() -> ContextProvider
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for ContextProvider
impl RefUnwindSafe for ContextProvider
impl Send for ContextProvider
impl Sync for ContextProvider
impl Unpin for ContextProvider
impl UnwindSafe for ContextProvider
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more