1
0

ssr.rs 956 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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::new(app);
  8. let _ = vdom.rebuild();
  9. println!("{}", dioxus_ssr::render(&vdom));
  10. // Or we can render rsx! calls themselves
  11. println!(
  12. "{}",
  13. dioxus_ssr::render_lazy(rsx! {
  14. div {
  15. h1 { "Hello, world!" }
  16. }
  17. })
  18. );
  19. // We can configure the SSR rendering to add ids for rehydration
  20. println!("{}", dioxus_ssr::pre_render(&vdom));
  21. // We can render to a buf directly too
  22. let mut file = String::new();
  23. let mut renderer = dioxus_ssr::Renderer::default();
  24. renderer.render_to(&mut file, &vdom).unwrap();
  25. println!("{}", file);
  26. }
  27. fn app(cx: Scope) -> Element {
  28. cx.render(rsx!(
  29. div {
  30. h1 { "Title" }
  31. p { "Body" }
  32. }
  33. ))
  34. }