add use_route hook

This commit is contained in:
Adrian Wannenmacher 2022-12-12 23:39:34 +01:00
parent 96c69b2437
commit 8df588046d
No known key found for this signature in database
GPG key ID: 19D986ECB1E492D5
2 changed files with 28 additions and 0 deletions

View 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
}
}
}

View file

@ -15,6 +15,9 @@ mod contexts {
pub mod hooks { pub mod hooks {
mod use_router; mod use_router;
pub use use_router::*; pub use use_router::*;
mod use_route;
pub use use_route::*;
} }
pub mod prelude { pub mod prelude {