1
0

hydrate.rs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. #[allow(non_snake_case)]
  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. tracing_wasm::set_as_global_default();
  44. let mut dom = VirtualDom::new(app);
  45. let _ = dom.rebuild();
  46. let pre = dioxus_ssr::pre_render(&dom);
  47. tracing::trace!("{}", 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, (), Config::new().hydrate(true));
  58. }