FerroUI/ docs

Search guides, controls, Rust APIs, and XAML.

    GitHub ↗

    router

    function · ferroui-view · v0.5.0

    fn router(initial: &str, routes: Vec<RouteDef>) -> crate::element::Element
    

    Provides a Router to the components below, with initial as the only path of its stack, and shows the view of the first route whose pattern matches the current path: the view of a last route route("*", ..) when no other matches, and nothing when none matches at all.

    router("/", vec![
        route("/", |_| rsx! { Home {} }),
        route("/people/:id", |m| rsx! { Person { id: m.param("id").unwrap_or_default().to_string() } }),
        route("*", |m| rsx! { NotFound { path: m.path().to_string() } }),
    ])
    

    The view of the current route is a component of its own that runs again when the path changes. Another route mounts its view anew, and the view of the route before is taken out with its state. The same route with other parameters runs its view again with the new RouteMatch, and the reconciler patches its controls. A component below finds the router with use_router while it runs, and keeps the handle for its handlers:

    #[component(rerender)]
    fn Home() -> Element {
        let router = use_router();
        rsx! { Button { on_click: move |_| router.navigate("/people/42"), "Open" } }
    }
    

    As any component, a router at the root of a mounted tree keeps the class of the control its views make; it is usually the content of a window or a child of a panel.

    View source

    Availability

    Extracted for aarch64-apple-darwin. Cargo features are listed in the crate index.