# router

Version: 0.5.0
Framework commit: d503df2befa756e681fcdc6250e9552dc7388bea
Canonical URL: /docs/0.5.0/api/ferroui-view/router/function-router/

**function** · [`ferroui-view`](/docs/0.5.0/api/ferroui-view/) · v0.5.0

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

Provides a [`Router`](/docs/0.5.0/api/ferroui-view/router/struct-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.

```ignore
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`](/docs/0.5.0/api/ferroui-view/router/struct-routematch/), and the
reconciler patches its controls. A component below finds the router with
[`use_router`](/docs/0.5.0/api/ferroui-view/router/function-use_router/) while it runs, and keeps the handle for its handlers:

```ignore
#[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](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.View/router.rs#L241)

## Availability

Extracted for `aarch64-apple-darwin`. Cargo features are listed in the [crate index](/docs/0.5.0/api/).
