ssr.rs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. fn main() {
  7. // We can render VirtualDoms
  8. let mut vdom = VirtualDom::new(app);
  9. let _ = vdom.rebuild();
  10. println!("{}", dioxus::ssr::render_vdom(&vdom));
  11. // Or we can render rsx! calls themselves
  12. println!(
  13. "{}",
  14. dioxus::ssr::render_lazy(rsx! {
  15. div {
  16. h1 { "Hello, world!" }
  17. }
  18. })
  19. );
  20. // We can configure the SSR rendering to add ids for rehydration
  21. println!(
  22. "{}",
  23. dioxus::ssr::render_vdom_cfg(&vdom, |c| c.pre_render(true))
  24. );
  25. // We can even render as a writer
  26. let mut file = String::new();
  27. let _ = file.write_fmt(format_args!(
  28. "{}",
  29. dioxus::ssr::TextRenderer::from_vdom(&vdom, Default::default())
  30. ));
  31. println!("{}", file);
  32. }
  33. fn app(cx: Scope) -> Element {
  34. cx.render(rsx!(
  35. div {
  36. h1 { "Title" }
  37. p { "Body" }
  38. }
  39. ))
  40. }