hydrate.rs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. use dioxus::prelude::*;
  2. use web_sys::window;
  3. fn app(cx: Scope) -> Element {
  4. cx.render(rsx! {
  5. div {
  6. h1 { "thing 1" }
  7. }
  8. div {
  9. h2 { "thing 2"}
  10. }
  11. div {
  12. h2 { "thing 2"}
  13. "asd"
  14. "asd"
  15. bapp()
  16. }
  17. (0..10).map(|f| rsx!{
  18. div {
  19. "thing {f}"
  20. }
  21. })
  22. })
  23. }
  24. fn bapp(cx: Scope) -> Element {
  25. cx.render(rsx! {
  26. div {
  27. h1 { "thing 1" }
  28. }
  29. div {
  30. h2 { "thing 2"}
  31. }
  32. div {
  33. h2 { "thing 2"}
  34. "asd"
  35. "asd"
  36. }
  37. })
  38. }
  39. fn main() {
  40. console_error_panic_hook::set_once();
  41. wasm_logger::init(wasm_logger::Config::new(log::Level::Trace));
  42. let mut dom = VirtualDom::new(app);
  43. let _ = dom.rebuild();
  44. let pre = dioxus_ssr::pre_render_vdom(&dom);
  45. log::trace!("{}", pre);
  46. // set the inner content of main to the pre-rendered content
  47. window()
  48. .unwrap()
  49. .document()
  50. .unwrap()
  51. .get_element_by_id("main")
  52. .unwrap()
  53. .set_inner_html(&pre);
  54. // now rehydtrate
  55. dioxus_web::launch_with_props(app, (), |c| c.hydrate(true));
  56. }