mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-14 08:27:07 +00:00
use_route hook can be used outside of Route components
This commit is contained in:
parent
0fdd6d268f
commit
1080ffe52d
1 changed files with 10 additions and 7 deletions
|
@ -12,9 +12,7 @@ pub fn use_route(cx: &ScopeState) -> &UseRoute {
|
||||||
.consume_context::<RouterService>()
|
.consume_context::<RouterService>()
|
||||||
.expect("Cannot call use_route outside the scope of a Router component");
|
.expect("Cannot call use_route outside the scope of a Router component");
|
||||||
|
|
||||||
let route_context = cx
|
let route_context = cx.consume_context::<RouteContext>();
|
||||||
.consume_context::<RouteContext>()
|
|
||||||
.expect("Cannot call use_route outside the scope of a Router component");
|
|
||||||
|
|
||||||
router.subscribe_onchange(cx.scope_id());
|
router.subscribe_onchange(cx.scope_id());
|
||||||
|
|
||||||
|
@ -36,7 +34,9 @@ pub fn use_route(cx: &ScopeState) -> &UseRoute {
|
||||||
/// A handle to the current location of the router.
|
/// A handle to the current location of the router.
|
||||||
pub struct UseRoute {
|
pub struct UseRoute {
|
||||||
pub(crate) route: Arc<ParsedRoute>,
|
pub(crate) route: Arc<ParsedRoute>,
|
||||||
pub(crate) route_context: RouteContext,
|
|
||||||
|
/// If `use_route` is used inside a `Route` component this has some context otherwise `None`.
|
||||||
|
pub(crate) route_context: Option<RouteContext>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UseRoute {
|
impl UseRoute {
|
||||||
|
@ -84,9 +84,12 @@ impl UseRoute {
|
||||||
/// `value.parse::<T>()`. This method returns `None` if the named
|
/// `value.parse::<T>()`. This method returns `None` if the named
|
||||||
/// parameter does not exist in the current path.
|
/// parameter does not exist in the current path.
|
||||||
pub fn segment(&self, name: &str) -> Option<&str> {
|
pub fn segment(&self, name: &str) -> Option<&str> {
|
||||||
let index = self
|
let total_route = match self.route_context {
|
||||||
.route_context
|
None => self.route.url.path(),
|
||||||
.total_route
|
Some(ref ctx) => &ctx.total_route,
|
||||||
|
};
|
||||||
|
|
||||||
|
let index = total_route
|
||||||
.trim_start_matches('/')
|
.trim_start_matches('/')
|
||||||
.split('/')
|
.split('/')
|
||||||
.position(|segment| segment.starts_with(':') && &segment[1..] == name)?;
|
.position(|segment| segment.starts_with(':') && &segment[1..] == name)?;
|
||||||
|
|
Loading…
Reference in a new issue