hydrate.rs 1.3 KB

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