main.rs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //! Run with:
  2. //!
  3. //! ```sh
  4. //! dx build --features web --release
  5. //! cargo run --features server
  6. //! ```
  7. #![allow(unused)]
  8. use dioxus::prelude::*;
  9. use dioxus_fullstack::{launch, prelude::*};
  10. use serde::{Deserialize, Serialize};
  11. // Generate all routes and output them to the docs path
  12. #[cfg(feature = "server")]
  13. #[tokio::main]
  14. async fn main() {
  15. let wrapper = DefaultRenderer {
  16. before_body: r#"<!DOCTYPE html>
  17. <html lang="en">
  18. <head>
  19. <meta charset="UTF-8">
  20. <meta name="viewport" content="width=device-width,
  21. initial-scale=1.0">
  22. <title>Dioxus Application</title>
  23. </head>
  24. <body>"#
  25. .to_string(),
  26. after_body: r#"</body>
  27. </html>"#
  28. .to_string(),
  29. };
  30. let mut renderer = IncrementalRenderer::builder().build();
  31. pre_cache_static_routes::<Route, _>(&mut renderer, &wrapper)
  32. .await
  33. .unwrap();
  34. }
  35. // Hydrate the page
  36. #[cfg(not(feature = "server"))]
  37. fn main() {
  38. #[cfg(all(feature = "web", not(feature = "server")))]
  39. dioxus_web::launch_with_props(
  40. dioxus_fullstack::router::RouteWithCfg::<Route>,
  41. dioxus_fullstack::prelude::get_root_props_from_document()
  42. .expect("Failed to get root props from document"),
  43. dioxus_web::Config::default().hydrate(true),
  44. );
  45. }
  46. #[derive(Clone, Routable, Debug, PartialEq, Serialize, Deserialize)]
  47. enum Route {
  48. #[route("/")]
  49. Home {},
  50. #[route("/blog")]
  51. Blog,
  52. }
  53. #[component]
  54. fn Blog() -> Element {
  55. rsx! {
  56. Link { to: Route::Home {}, "Go to counter" }
  57. table {
  58. tbody {
  59. for _ in 0..100 {
  60. tr {
  61. for _ in 0..100 {
  62. td { "hello world!" }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }
  70. #[component]
  71. fn Home() -> Element {
  72. let mut count = use_signal(|| 0);
  73. let text = use_signal(|| "...".to_string());
  74. rsx! {
  75. Link { to: Route::Blog {}, "Go to blog" }
  76. div {
  77. h1 { "High-Five counter: {count}" }
  78. button { onclick: move |_| count += 1, "Up high!" }
  79. button { onclick: move |_| count -= 1, "Down low!" }
  80. }
  81. }
  82. }