hydrate.rs 1.4 KB

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