hydration.rs 1.2 KB

123456789101112131415161718192021222324252627282930313233
  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. fn main() {
  13. let vdom = VirtualDom::new(app);
  14. let content = dioxus_ssr::render_vdom_cfg(&vdom, |f| f.pre_render(true));
  15. dioxus_desktop::launch_cfg(app, |c| c.with_prerendered(content));
  16. }
  17. fn app(cx: Scope) -> Element {
  18. let val = use_state(&cx, || 0);
  19. cx.render(rsx! {
  20. div {
  21. h1 { "hello world. Count: {val}" }
  22. button {
  23. onclick: move |_| *val.make_mut() += 1,
  24. "click to increment"
  25. }
  26. }
  27. })
  28. }