hydration.rs 1.2 KB

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