hydrate.rs 1.3 KB

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