hydrate.rs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. use dioxus::prelude::*;
  2. use dioxus_web::Config;
  3. use web_sys::window;
  4. fn app() -> Element {
  5. rsx! {
  6. div { h1 { "thing 1" } }
  7. div { h2 { "thing 2" } }
  8. div {
  9. h2 { "thing 2" }
  10. "asd"
  11. "asd"
  12. Bapp {}
  13. }
  14. {(0..10).map(|f| rsx!{
  15. div {
  16. "thing {f}"
  17. }
  18. })}
  19. }
  20. }
  21. #[allow(non_snake_case)]
  22. fn Bapp() -> Element {
  23. rsx! {
  24. div { h1 { "thing 1" } }
  25. div { h2 { "thing 2" } }
  26. div {
  27. h2 { "thing 2" }
  28. "asd"
  29. "asd"
  30. }
  31. }
  32. }
  33. fn main() {
  34. console_error_panic_hook::set_once();
  35. tracing_wasm::set_as_global_default();
  36. let mut dom = VirtualDom::new(app);
  37. dom.rebuild(&mut dioxus_core::NoOpMutations);
  38. let pre = dioxus_ssr::pre_render(&dom);
  39. tracing::trace!("{}", pre);
  40. // set the inner content of main to the pre-rendered content
  41. window()
  42. .unwrap()
  43. .document()
  44. .unwrap()
  45. .get_element_by_id("main")
  46. .unwrap()
  47. .set_inner_html(&pre);
  48. // now rehydrate
  49. dioxus_web::launch::launch(app, vec![], Config::new().hydrate(true));
  50. }