dioxus/examples/router.rs

65 lines
1.7 KiB
Rust
Raw Normal View History

2021-11-03 04:35:56 +00:00
use dioxus::prelude::*;
use dioxus_router::{use_router, Link};
2021-11-03 04:35:56 +00:00
#[derive(PartialEq, Debug, Clone)]
pub enum Route {
// #[at("/")]
Home,
// #[at("/:id")]
AllUsers { page: u32 },
// #[at("/:id")]
User { id: u32 },
// #[at("/:id")]
BlogList { page: u32 },
// #[at("/:id")]
BlogPost { post_id: u32 },
// #[at("/404")]
// #[not_found]
NotFound,
}
fn app(cx: Scope) -> Element {
let route = use_router(&cx, Route::parse);
2021-11-03 04:35:56 +00:00
cx.render(rsx! {
nav {
Link { to: Route::Home, "Go home!" }
Link { to: Route::AllUsers { page: 0 }, "List all users" }
Link { to: Route::BlogList { page: 0 }, "Blog posts" }
}
match route {
2021-11-03 04:35:56 +00:00
Route::Home => rsx!("Home"),
Route::AllUsers { page } => rsx!("All users - page {page}"),
Route::User { id } => rsx!("User - id: {id}"),
Route::BlogList { page } => rsx!("Blog posts - page {page}"),
Route::BlogPost { post_id } => rsx!("Blog post - post {post_id}"),
Route::NotFound => rsx!("Not found"),
}
2021-11-03 04:35:56 +00:00
footer {}
})
}
2021-11-03 04:35:56 +00:00
impl Route {
// Generate the appropriate route from the "tail" end of the URL
fn parse(url: &str) -> Self {
use Route::*;
match url {
"/" => Home,
"/users" => AllUsers { page: 1 },
"/users/:page" => AllUsers { page: 1 },
"/users/:page/:id" => User { id: 1 },
"/blog" => BlogList { page: 1 },
"/blog/:page" => BlogList { page: 1 },
"/blog/:page/:id" => BlogPost { post_id: 1 },
_ => NotFound,
}
}
}
2021-11-03 19:13:50 +00:00
fn main() {}