dioxus/packages/router/examples/simple.rs

53 lines
1.3 KiB
Rust
Raw Normal View History

2022-02-10 21:00:15 -05:00
#![allow(non_snake_case)]
2021-11-19 00:49:04 -05: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 15:22:42 -05:00
wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
2022-03-02 17:57:57 -05:00
dioxus_web::launch(app);
2021-11-19 00:49:04 -05:00
}
2022-03-02 17:57:57 -05:00
fn app(cx: Scope) -> Element {
cx.render(rsx! {
Router {
2022-03-02 17:57:57 -05: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 14:06:54 -05:00
Link { to: "/apples", li { "go to apples" }}
}
2022-03-02 17:57:57 -05:00
Route { to: "/", Home {} }
Route { to: "/blog/", BlogList {} }
Route { to: "/blog/:id/", BlogPost {} }
2022-03-02 22:35:57 -05:00
Route { to: "/oranges", "Oranges are not apples!" }
Redirect { from: "/apples", to: "/oranges" }
2021-12-28 23:20:01 -05:00
}
})
2022-03-02 17:57:57 -05:00
}
2021-11-19 00:49:04 -05:00
fn Home(cx: Scope) -> Element {
cx.render(rsx! {
2022-02-10 21:00:15 -05:00
h1 { "Home" }
})
}
2021-11-22 15:22:42 -05:00
fn BlogList(cx: Scope) -> Element {
2021-11-22 15:22:42 -05:00
cx.render(rsx! {
2022-02-10 21:00:15 -05:00
div { "Blog List" }
2021-11-22 15:22:42 -05:00
})
}
fn BlogPost(cx: Scope) -> Element {
2022-03-02 22:35:57 -05:00
let id = use_route(&cx).segment("id")?;
log::debug!("rendering blog post {}", id);
2022-02-10 21:00:15 -05:00
cx.render(rsx! { div { "{id:?}" } })
}