navigator.rs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #![allow(non_snake_case)]
  2. use dioxus::prelude::*;
  3. use dioxus_router::prelude::*;
  4. #[derive(Routable, Clone)]
  5. #[rustfmt::skip]
  6. enum Route {
  7. #[route("/")]
  8. Home {},
  9. #[route("/:..route")]
  10. PageNotFound { route: Vec<String> },
  11. }
  12. #[inline_props]
  13. fn App(cx: Scope) -> Element {
  14. render! {
  15. Router {}
  16. }
  17. }
  18. // ANCHOR: nav
  19. #[inline_props]
  20. fn Home(cx: Scope) -> Element {
  21. let nav = use_navigator(cx);
  22. // push
  23. nav.push(Route::PageNotFound { route: vec![] });
  24. // replace
  25. nav.replace(Route::Home {});
  26. // go back
  27. nav.go_back();
  28. // go forward
  29. nav.go_forward();
  30. render! {
  31. h1 { "Welcome to the Dioxus Blog!" }
  32. }
  33. }
  34. // ANCHOR_END: nav
  35. #[inline_props]
  36. fn PageNotFound(cx: Scope, route: Vec<String>) -> Element {
  37. render! {
  38. h1 { "Page not found" }
  39. p { "We are terribly sorry, but the page you requested doesn't exist." }
  40. pre {
  41. color: "red",
  42. "log:\nattemped to navigate to: {route:?}"
  43. }
  44. }
  45. }
  46. fn main() {}