dioxus/examples/router.rs

68 lines
1.6 KiB
Rust
Raw Normal View History

2022-01-05 22:30:12 +00:00
#![allow(non_snake_case)]
2021-11-03 04:35:56 +00:00
2022-01-05 22:30:12 +00:00
use dioxus::prelude::*;
use dioxus_router::{Link, Route, Router};
2022-01-11 17:27:02 +00:00
use serde::Deserialize;
2021-11-03 04:35:56 +00:00
2022-01-05 22:30:12 +00:00
fn main() {
dioxus_desktop::launch(app);
2021-11-03 04:35:56 +00:00
}
fn app(cx: Scope) -> Element {
2021-11-03 04:35:56 +00:00
cx.render(rsx! {
2022-01-05 22:30:12 +00:00
Router {
ul {
Link { to: "/", li { "Go home!" } }
2022-03-05 20:25:09 +00:00
Link { to: "/users", li { "List all users" } }
Link { to: "/blog", li { "Blog posts" } }
Link { to: "/users/bill", li { "List all users" } }
Link { to: "/blog/5", li { "Blog post 5" } }
2022-01-05 22:30:12 +00:00
}
Route { to: "/", "Home" }
2022-03-05 20:25:09 +00:00
Route { to: "/users", "User list" }
Route { to: "/users/:name", User {} }
Route { to: "/blog", "Blog list" }
Route { to: "/blog/:post", BlogPost {} }
2022-01-05 22:30:12 +00:00
Route { to: "", "Err 404 Route Not Found" }
}
2021-11-03 04:35:56 +00:00
})
}
2021-11-03 04:35:56 +00:00
2022-01-05 22:30:12 +00:00
fn BlogPost(cx: Scope) -> Element {
let post = dioxus_router::use_route(cx).last_segment().unwrap();
2022-01-05 22:30:12 +00:00
cx.render(rsx! {
div {
h1 { "Reading blog post: {post}" }
p { "example blog post" }
2021-11-03 04:35:56 +00:00
}
2022-01-05 22:30:12 +00:00
})
2021-11-03 04:35:56 +00:00
}
2022-01-11 17:27:02 +00:00
#[derive(Deserialize)]
struct Query {
bold: bool,
}
2022-01-05 22:30:12 +00:00
fn User(cx: Scope) -> Element {
let post = dioxus_router::use_route(cx).last_segment().unwrap();
2022-03-03 04:03:10 +00:00
let query = dioxus_router::use_route(cx)
2022-01-25 00:52:12 +00:00
.query::<Query>()
.unwrap_or(Query { bold: false });
2022-01-05 22:30:12 +00:00
cx.render(rsx! {
div {
h1 { "Reading blog post: {post}" }
p { "example blog post" }
2022-01-25 00:54:27 +00:00
2022-01-25 00:52:12 +00:00
if query.bold {
rsx!{ b { "bold" } }
} else {
rsx!{ i { "italic" } }
}
2022-01-05 22:30:12 +00:00
}
})
}