simple_desktop.rs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #![allow(non_snake_case)]
  2. use dioxus::prelude::*;
  3. use dioxus_router::*;
  4. fn main() {
  5. simple_logger::SimpleLogger::new()
  6. .with_level(log::LevelFilter::Debug)
  7. .with_module_level("dioxus_router", log::LevelFilter::Trace)
  8. .with_module_level("dioxus", log::LevelFilter::Trace)
  9. .init()
  10. .unwrap();
  11. dioxus_desktop::launch(app);
  12. }
  13. fn app(cx: Scope) -> Element {
  14. cx.render(rsx! {
  15. Router {
  16. h1 { "Your app here" }
  17. ul {
  18. Link { to: "/", li { "home" } }
  19. Link { to: "/blog", li { "blog" } }
  20. Link { to: "/blog/tim", li { "tims' blog" } }
  21. Link { to: "/blog/bill", li { "bills' blog" } }
  22. Link { to: "/blog/james",
  23. li { "james amazing' blog" }
  24. }
  25. Link { to: "/apples", li { "go to apples" } }
  26. }
  27. Route { to: "/", Home {} }
  28. Route { to: "/blog/", BlogList {} }
  29. Route { to: "/blog/:id/", BlogPost {} }
  30. Route { to: "/oranges", "Oranges are not apples!" }
  31. Redirect { from: "/apples", to: "/oranges" }
  32. }
  33. })
  34. }
  35. fn Home(cx: Scope) -> Element {
  36. log::debug!("rendering home {:?}", cx.scope_id());
  37. cx.render(rsx! { h1 { "Home" } })
  38. }
  39. fn BlogList(cx: Scope) -> Element {
  40. log::debug!("rendering blog list {:?}", cx.scope_id());
  41. cx.render(rsx! { div { "Blog List" } })
  42. }
  43. fn BlogPost(cx: Scope) -> Element {
  44. let Some(id) = use_route(cx).segment("id") else {
  45. return cx.render(rsx! { div { "No blog post id" } })
  46. };
  47. log::debug!("rendering blog post {}", id);
  48. cx.render(rsx! {
  49. div {
  50. h3 { "blog post: {id:?}" }
  51. Link { to: "/blog/", "back to blog list" }
  52. }
  53. })
  54. }