hydration.rs 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. //! Example: realworld 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(&vdom, |f| f.pre_render(true));
  16. dioxus::desktop::launch(App, |c| c.with_prerendered(content)).unwrap();
  17. }
  18. static App: FC<()> = |cx| {
  19. let mut val = use_state(cx, || 0);
  20. cx.render(rsx! {
  21. div {
  22. h1 {"hello world. Count: {val}"}
  23. button {
  24. "click to increment"
  25. onclick: move |_| val += 1
  26. }
  27. }
  28. })
  29. };