dioxus/packages/router/examples/simple.rs

53 lines
1.3 KiB
Rust
Raw Normal View History

2022-02-11 02:00:15 +00:00
#![allow(non_snake_case)]
2021-11-19 05:49:04 +00:00
use dioxus_core::prelude::*;
use dioxus_core_macro::*;
use dioxus_html as dioxus_elements;
use dioxus_router::*;
fn main() {
console_error_panic_hook::set_once();
2021-11-22 20:22:42 +00:00
wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
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! {
Router {
2022-03-02 22:57:57 +00:00
h1 { "Your app here" }
ul {
Link { to: "/", li { "home" }}
Link { to: "/blog", li { "blog" }}
Link { to: "/blog/tim", li { "tims' blog" }}
Link { to: "/blog/bill", li { "bills' blog" }}
2022-03-05 19:06:54 +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 {
cx.render(rsx! {
2022-02-11 02:00:15 +00:00
h1 { "Home" }
})
}
2021-11-22 20:22:42 +00:00
fn BlogList(cx: Scope) -> Element {
2021-11-22 20:22:42 +00:00
cx.render(rsx! {
2022-02-11 02:00:15 +00:00
div { "Blog List" }
2021-11-22 20:22:42 +00:00
})
}
fn BlogPost(cx: Scope) -> Element {
2022-03-03 03:35:57 +00:00
let id = use_route(&cx).segment("id")?;
2022-03-27 15:21:57 +00:00
log::trace!("rendering blog post {}", id);
2022-03-03 03:35:57 +00:00
2022-02-11 02:00:15 +00:00
cx.render(rsx! { div { "{id:?}" } })
}