hydration.rs 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. //! Example: real-world usage of hydration
  2. //! ------------------------------------
  3. //!
  4. //! This example shows how to pre-render a page using dioxus SSR and then how to rehydrate it on the client side.
  5. //!
  6. //! To accomplish hydration on the web, you'll want to set up a slightly more sophisticated build & bundle strategy. In
  7. //! the official docs, we have a guide for using DioxusStudio as a build tool with pre-rendering and hydration.
  8. //!
  9. //! In this example, we pre-render the page to HTML and then pass it into the desktop configuration. This serves as a
  10. //! proof-of-concept for the hydration feature, but you'll probably only want to use hydration for the web.
  11. use dioxus::prelude::*;
  12. use dioxus::ssr;
  13. fn main() {
  14. let vdom = VirtualDom::new(app);
  15. let content = ssr::render_vdom_cfg(&vdom, |f| f.pre_render(true));
  16. dioxus::desktop::launch_cfg(app, |c| c.with_prerendered(content));
  17. }
  18. fn app(cx: Scope) -> Element {
  19. let (val, set_val) = use_state(&cx, || 0);
  20. cx.render(rsx! {
  21. div {
  22. h1 { "hello world. Count: {val}" }
  23. button {
  24. onclick: move |_| set_val(val + 1),
  25. "click to increment"
  26. }
  27. }
  28. })
  29. }