pub struct Context;Expand description
A marker type for context operations.
Provides static methods for storing and retrieving context values in the reactive scope hierarchy.
§Example
let scope = Scope::new();
scope.enter(|| {
Context::provide(42i32);
Context::provide(String::from("Hello"));
assert_eq!(Context::get::<i32>(), Some(42));
assert_eq!(Context::get::<String>(), Some(String::from("Hello")));
});Implementations§
Source§impl Context
impl Context
Sourcepub fn provide<T>(value: T)where
T: Clone + 'static,
pub fn provide<T>(value: T)where
T: Clone + 'static,
Store a context value in the current scope.
The stored context value can be retrieved by the current scope and any of its
descendants using Context::get. Child scopes can provide their own values
of the same type, which will shadow the parent’s value for that subtree.
Context values are automatically cleaned up when the scope is disposed.
§Example
let scope = Scope::new();
scope.enter(|| {
Context::provide(42i32);
assert_eq!(Context::get::<i32>(), Some(42));
});Sourcepub fn get<T>() -> Option<T>where
T: Clone + 'static,
pub fn get<T>() -> Option<T>where
T: Clone + 'static,
Try to retrieve a stored context value from the current scope or its ancestors.
Context lookup walks up the scope tree from the current scope to find the nearest ancestor that provides a value of the requested type. This enables nested components to override context values for their subtrees.
§Example
let parent = Scope::new();
parent.enter(|| {
Context::provide(42i32);
let child = parent.create_child();
child.enter(|| {
// Child sees parent's context
assert_eq!(Context::get::<i32>(), Some(42));
});
});