docs: clarify difference between use_context and consume_context (#5406)
* docs: clarify difference between use_context and consume_context Document the key distinction: use_context is a hook that captures the context value once on first render and caches it, following the rules of hooks. consume_context reads directly from the runtime each time and can be called from anywhere (event handlers, async tasks, etc.). Fixes #3862 * fix: remove redundant explicit rustdoc link targets * fix: use plain text for cross-crate use_context reference in rustdoc
This commit is contained in:
1 parent
7289256b0a
commit
cece12b57e
2 files changed
+29
-6
No files matched your search
@@ -30,12 +30,22 @@ pub fn throw_error(error: impl Into<CapturedError> + 'static) {
|
||||
Runtime::with(|rt| rt.throw_error(rt.current_scope_id(), error))
|
||||
}
|
||||
|
||||
/// Consume context from the current scope
|
||||
/// Try to consume context from the current scope, returning `None` if not found.
|
||||
///
|
||||
/// Unlike `use_context`, this is **not** a hook. It reads the context value directly from the
|
||||
/// runtime each time it is called, rather than caching it on first render. This means it can be
|
||||
/// called from anywhere the Dioxus runtime is active — inside event handlers, async tasks, spawned
|
||||
/// futures, or other non-hook contexts — without following the rules of hooks.
|
||||
pub fn try_consume_context<T: 'static + Clone>() -> Option<T> {
|
||||
Runtime::with_current_scope(|cx| cx.consume_context::<T>())
|
||||
}
|
||||
|
||||
/// Consume context from the current scope
|
||||
/// Consume context from the current scope, panicking if not found.
|
||||
///
|
||||
/// Unlike `use_context`, this is **not** a hook. It reads the context value directly from the
|
||||
/// runtime each time it is called, rather than caching it on first render. This means it can be
|
||||
/// called from anywhere the Dioxus runtime is active — inside event handlers, async tasks, spawned
|
||||
/// futures, or other non-hook contexts — without following the rules of hooks.
|
||||
pub fn consume_context<T: 'static + Clone>() -> T {
|
||||
Runtime::with_current_scope(|cx| cx.consume_context::<T>())
|
||||
.unwrap_or_else(|| panic!("Could not find context {}", std::any::type_name::<T>()))
|
||||
|
||||
Reference in new issue
Block a user