mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-27 14:40:44 +00:00
add use_route hook
This commit is contained in:
parent
96c69b2437
commit
8df588046d
2 changed files with 28 additions and 0 deletions
25
packages/router/src/hooks/use_route.rs
Normal file
25
packages/router/src/hooks/use_route.rs
Normal file
|
@ -0,0 +1,25 @@
|
|||
use async_rwlock::RwLockReadGuard;
|
||||
use dioxus::{core::Component, prelude::ScopeState};
|
||||
use dioxus_router_core::RouterState;
|
||||
use log::error;
|
||||
|
||||
use crate::utils::use_router_internal::use_router_internal;
|
||||
|
||||
#[must_use]
|
||||
pub fn use_route<'a>(cx: &'a ScopeState) -> Option<RwLockReadGuard<'a, RouterState<Component>>> {
|
||||
match use_router_internal(cx) {
|
||||
Some(r) => loop {
|
||||
if let Some(s) = r.state.try_read() {
|
||||
break Some(s);
|
||||
}
|
||||
},
|
||||
None => {
|
||||
let msg = "`use_route` must have access to a parent router";
|
||||
error!("{msg}, will be inactive");
|
||||
#[cfg(debug_assertions)]
|
||||
panic!("{}", msg);
|
||||
#[cfg(not(debug_assertions))]
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
|
@ -15,6 +15,9 @@ mod contexts {
|
|||
pub mod hooks {
|
||||
mod use_router;
|
||||
pub use use_router::*;
|
||||
|
||||
mod use_route;
|
||||
pub use use_route::*;
|
||||
}
|
||||
|
||||
pub mod prelude {
|
||||
|
|
Loading…
Reference in a new issue