hydration.rs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  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::desktop::Config;
  12. use dioxus::prelude::*;
  13. fn main() {
  14. dioxus::LaunchBuilder::desktop()
  15. .with_cfg(Config::new().with_prerendered({
  16. // We build the dom a first time, then pre-render it to HTML
  17. let pre_rendered_dom = VirtualDom::prebuilt(app);
  18. // We then launch the app with the pre-rendered HTML
  19. dioxus_ssr::pre_render(&pre_rendered_dom)
  20. }))
  21. .launch(app)
  22. }
  23. fn app() -> Element {
  24. let mut val = use_signal(|| 0);
  25. rsx! {
  26. div {
  27. h1 { "hello world. Count: {val}" }
  28. button { onclick: move |_| val += 1, "click to increment" }
  29. }
  30. }
  31. }