ssr.rs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //! Example: SSR
  2. //!
  3. //! This example shows how we can render the Dioxus Virtualdom using SSR.
  4. use std::fmt::Write;
  5. use dioxus::prelude::*;
  6. use dioxus_ssr::config::Config;
  7. fn main() {
  8. // We can render VirtualDoms
  9. let mut vdom = VirtualDom::new(app);
  10. let _ = vdom.rebuild();
  11. println!("{}", dioxus_ssr::render_vdom(&vdom));
  12. // Or we can render rsx! calls themselves
  13. println!(
  14. "{}",
  15. dioxus_ssr::render_lazy(rsx! {
  16. div {
  17. h1 { "Hello, world!" }
  18. }
  19. })
  20. );
  21. // We can configure the SSR rendering to add ids for rehydration
  22. println!(
  23. "{}",
  24. dioxus_ssr::render_vdom_cfg(&vdom, Config::default().pre_render(true))
  25. );
  26. // We can even render as a writer
  27. let mut file = String::new();
  28. let _ = file.write_fmt(format_args!(
  29. "{}",
  30. dioxus_ssr::SsrRender::default().render_vdom(&vdom)
  31. ));
  32. println!("{}", file);
  33. }
  34. fn app(cx: Scope) -> Element {
  35. cx.render(rsx!(
  36. div {
  37. h1 { "Title" }
  38. p { "Body" }
  39. }
  40. ))
  41. }