Skip to main content

use_context

Function use_context 

Source
pub fn use_context<T>() -> Option<T>
where T: Clone + 'static,
๐Ÿ‘ŽDeprecated since 0.2.0: Use Context::get instead; this will be removed in a future release
Expand description

Try to retrieve a stored Context value in the reactive system.

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

In a parent component:

provide_context(42);
provide_context(String::from("Hello world"));

And so in a child component you can retrieve each context data by specifying the type:

let foo: Option<i32> = use_context();
let bar: Option<String> = use_context();