ssr.rs 914 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. //! Example: SSR
  2. //!
  3. //! This example shows how we can render the Dioxus Virtualdom using SSR.
  4. use dioxus::prelude::*;
  5. fn main() {
  6. // We can render VirtualDoms
  7. let mut vdom = VirtualDom::prebuilt(app);
  8. println!("{}", dioxus_ssr::render(&vdom));
  9. // Or we can render rsx! calls themselves
  10. println!(
  11. "{}",
  12. dioxus_ssr::render_element(rsx! {
  13. div {
  14. h1 { "Hello, world!" }
  15. }
  16. })
  17. );
  18. // We can configure the SSR rendering to add ids for rehydration
  19. println!("{}", dioxus_ssr::pre_render(&vdom));
  20. // We can render to a buf directly too
  21. let mut file = String::new();
  22. let mut renderer = dioxus_ssr::Renderer::default();
  23. renderer.render_to(&mut file, &vdom).unwrap();
  24. println!("{file}");
  25. }
  26. fn app() -> Element {
  27. rsx!(
  28. div {
  29. h1 { "Title" }
  30. p { "Body" }
  31. }
  32. )
  33. }