Feat: add global context functions

Add functions like window() and router() to
allow dynamically grabbing global contexts
without have to use the hook variants.

Deprecates the existing hook variants to
discourage folks from adding more noise
to their codebases.
This commit is contained in:
Jonathan Kelley committed 2023-10-23 16:26:10 -04:00
1 parent cea9563e25
commit dd4547d753
16 files changed
+77 -78

No files matched your search

@@ -49,6 +49,7 @@ use crate::prelude::{Navigator, RouterContext};
/// # let _ = vdom.rebuild();
/// ```
#[must_use]
#[deprecated = "Prefer acquiring the router directly with `dioxus_router::router()`"]
pub fn use_navigator(cx: &ScopeState) -> &Navigator {
&*cx.use_hook(|| {
let router = cx
+6 -1
View File
@@ -2,7 +2,7 @@ use dioxus::prelude::ScopeState;
use crate::{prelude::RouterContext, utils::use_router_internal::use_router_internal};
#[deprecated = "prefer the use_navigator or use_route functions"]
#[deprecated = "prefer the `router()` function or `use_route` functions"]
#[must_use]
/// A hook that provides access to information about the router.
pub fn use_router(cx: &ScopeState) -> &RouterContext {
@@ -10,3 +10,8 @@ pub fn use_router(cx: &ScopeState) -> &RouterContext {
.as_ref()
.expect("use_route must have access to a router")
}
/// Aquire the router without subscribing to updates.
pub fn router() -> RouterContext {
dioxus::core::prelude::consume_context().unwrap()
}