simple.rs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #![allow(non_snake_case)]
  2. use dioxus::prelude::*;
  3. use dioxus_router::*;
  4. fn main() {
  5. console_error_panic_hook::set_once();
  6. wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
  7. dioxus_web::launch(app);
  8. }
  9. fn app(cx: Scope) -> Element {
  10. cx.render(rsx! {
  11. Router {
  12. h1 { "Your app here" }
  13. ul {
  14. Link { to: "/", li { "home" }}
  15. Link { to: "/blog", li { "blog" }}
  16. Link { to: "/blog/tim", li { "tims' blog" }}
  17. Link { to: "/blog/bill", li { "bills' blog" }}
  18. Link { to: "/apples", li { "go to apples" }}
  19. }
  20. Route { to: "/", Home {} }
  21. Route { to: "/blog/", BlogList {} }
  22. Route { to: "/blog/:id/", BlogPost {} }
  23. Route { to: "/oranges", "Oranges are not apples!" }
  24. Redirect { from: "/apples", to: "/oranges" }
  25. }
  26. })
  27. }
  28. fn Home(cx: Scope) -> Element {
  29. cx.render(rsx! {
  30. h1 { "Home" }
  31. })
  32. }
  33. fn BlogList(cx: Scope) -> Element {
  34. cx.render(rsx! {
  35. div { "Blog List" }
  36. })
  37. }
  38. fn BlogPost(cx: Scope) -> Element {
  39. let id = use_route(&cx).segment("id")?;
  40. log::trace!("rendering blog post {}", id);
  41. cx.render(rsx! { div { "{id:?}" } })
  42. }