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:
16 files changed
+77
-78
No files matched your search
@@ -1,5 +1,19 @@
|
||||
use crate::prelude::{ExternalNavigationFailure, IntoRoutable, RouterContext};
|
||||
|
||||
/// Acquire the navigator without subscribing to updates.
|
||||
///
|
||||
/// Can be called anywhere in the application provided a Router has been initialized.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// Panics if there is no router present.
|
||||
pub fn navigator() -> Navigator {
|
||||
Navigator(
|
||||
dioxus::core::prelude::consume_context::<RouterContext>()
|
||||
.expect("A router must be present to use navigator"),
|
||||
)
|
||||
}
|
||||
|
||||
/// A view into the navigation state of a router.
|
||||
#[derive(Clone)]
|
||||
pub struct Navigator(pub(crate) RouterContext);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
@@ -53,6 +53,8 @@ pub mod hooks {
|
||||
pub use use_navigator::*;
|
||||
}
|
||||
|
||||
pub use hooks::router;
|
||||
|
||||
/// A collection of useful items most applications might need.
|
||||
pub mod prelude {
|
||||
pub use crate::components::*;
|
||||
|
||||
Reference in new issue
Block a user