Function use_navigator

Source
pub fn use_navigator() -> Navigator
Expand description

A hook that provides access to the navigator to change the router history.

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

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

#[component]
fn App() -> Element {
    rsx! {
        Router::<Route> {}
    }
}

#[component]
fn Index() -> Element {
    let navigator = use_navigator();

    rsx! {
        button {
            onclick: move |_| { navigator.push(Route::Dynamic { id: 1234 }); },
            "Go to /1234"
        }
    }
}

#[component]
fn Dynamic(id: usize) -> Element {
    rsx! {
        p {
            "Current ID: {id}"
        }
    }
}