Function dioxus_router::hooks::use_route

source ·
pub fn use_route<R: Routable + Clone>(cx: &ScopeState) -> Option<R>
Expand description

A hook that provides access to information about the current routing location.

The Routable macro will define a version of this hook with an explicit type.

Return values

  • None, when not called inside a Link component.
  • Otherwise the current route.

Panic

  • When the calling component is not nested within a Link component durring a debug build.

Example


#[derive(Clone, Routable)]
enum Route {
    #[route("/")]
    Index {},
}

fn App(cx: Scope) -> Element {
    render! {
        h1 { "App" }
        Router::<Route> {}
    }
}

#[inline_props]
fn Index(cx: Scope) -> Element {
    let path = use_route(&cx).unwrap();
    render! {
        h2 { "Current Path" }
        p { "{path}" }
    }
}