dioxus/packages/router/examples/simple.rs

53 lines
1.4 KiB
Rust
Raw Normal View History

2022-02-11 02:00:15 +00:00
#![allow(non_snake_case)]
2022-06-25 13:27:10 +00:00
use dioxus::prelude::*;
2021-11-19 05:49:04 +00:00
use dioxus_router::*;
fn main() {
2022-03-02 22:57:57 +00:00
dioxus_web::launch(app);
2021-11-19 05:49:04 +00:00
}
2022-03-02 22:57:57 +00:00
fn app(cx: Scope) -> Element {
cx.render(rsx! {
2022-07-07 06:52:37 +00:00
Router {
2022-03-02 22:57:57 +00:00
h1 { "Your app here" }
ul {
2022-07-07 06:06:50 +00:00
Link { to: "/", li { "home" } }
Link { to: "/blog", li { "blog" } }
Link { to: "/blog/tim", li { "tims' blog" } }
Link { to: "/blog/bill", li { "bills' blog" } }
2022-12-13 22:44:47 +00:00
Link { to: "/blog/james", li { "james amazing' blog" } }
2022-07-07 06:06:50 +00:00
Link { to: "/apples", li { "go to apples" } }
}
2022-03-02 22:57:57 +00:00
Route { to: "/", Home {} }
Route { to: "/blog/", BlogList {} }
Route { to: "/blog/:id/", BlogPost {} }
2022-03-03 03:35:57 +00:00
Route { to: "/oranges", "Oranges are not apples!" }
Redirect { from: "/apples", to: "/oranges" }
2021-12-29 04:20:01 +00:00
}
})
2022-03-02 22:57:57 +00:00
}
2021-11-19 05:49:04 +00:00
fn Home(cx: Scope) -> Element {
2022-07-07 06:06:50 +00:00
cx.render(rsx! { h1 { "Home" } })
}
2021-11-22 20:22:42 +00:00
fn BlogList(cx: Scope) -> Element {
2022-07-07 06:06:50 +00:00
cx.render(rsx! { div { "Blog List" } })
}
fn BlogPost(cx: Scope) -> Element {
let Some(id) = use_route(cx).segment("id") else {
return cx.render(rsx! { div { "No blog post id" } })
};
2022-03-03 03:35:57 +00:00
2022-03-27 15:21:57 +00:00
log::trace!("rendering blog post {}", id);
2022-03-03 03:35:57 +00:00
2022-12-13 22:44:47 +00:00
cx.render(rsx! {
div {
h3 { "blog post: {id:?}" }
Link { to: "/blog/", "back to blog list" }
}
})
}