dioxus/packages/router
2021-12-28 23:20:01 -05:00
..
examples feat: more API updates 2021-12-28 23:20:01 -05:00
src feat: more API updates 2021-12-28 23:20:01 -05:00
Cargo.toml wip: miri stress tets 2021-12-18 23:03:59 -05:00
CHANGELOG.md Release dioxus-core v0.1.3, dioxus-core-macro v0.1.2, dioxus-html v0.1.0, dioxus-desktop v0.0.0, dioxus-hooks v0.1.3, dioxus-liveview v0.1.0, dioxus-mobile v0.0.0, dioxus-router v0.1.0, dioxus-ssr v0.1.0, dioxus-web v0.0.0, dioxus v0.1.1 2021-12-15 16:07:09 -05:00
README.md docs: update local examples and docs to support new syntaxes 2021-12-15 15:56:53 -05:00

Router hook for Dioxus apps

Dioxus-router provides a use_router hook that returns a different value depending on the route. The router is generic over any value, however it makes sense to return a different set of VNodes and feed them into the App's return VNodes.

Using the router should feel similar to tide's routing framework where an "address" book is assembled at the head of the app.

Here's an example of how to use the router hook:

#[derive(Clone, PartialEq, Serialize, Deserialize, Routable)]
enum AppRoute {
    Home, 
    Posts,
    NotFound
}

static App: Component<()> = |cx| {
    let route = use_router(cx, AppRoute::parse);
    
    match route {
        AppRoute::Home => rsx!(cx, Home {})
        AppRoute::Posts => rsx!(cx, Posts {})
        AppRoute::Notfound => rsx!(cx, Notfound {})
    }
};

Adding links into your app:

static Leaf: Component<()> = |cx| {
    rsx!(cx, div { 
        Link { to: AppRoute::Home } 
    })
}

Currently, the router is only supported in a web environment, but we plan to add 1st-party support via the context API when new renderers are available.