async.rs 860 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. use dioxus::prelude::*;
  2. use dioxus_core as dioxus;
  3. use dioxus_core_macro::*;
  4. use dioxus_hooks::*;
  5. use dioxus_html as dioxus_elements;
  6. use std::time::Duration;
  7. fn main() {
  8. dioxus_desktop::launch(app);
  9. }
  10. fn app(cx: Scope<()>) -> Element {
  11. let count = use_state(&cx, || 0);
  12. // push the futureo on initialization
  13. cx.use_hook(
  14. |_| {
  15. cx.push_future({
  16. let count = count.for_async();
  17. async move {
  18. tokio::time::sleep(Duration::from_millis(1000)).await;
  19. *count.get_mut() += 1;
  20. }
  21. });
  22. },
  23. |_| {},
  24. );
  25. cx.render(rsx! {
  26. div {
  27. h1 { "High-Five counter: {count}" }
  28. button {
  29. onclick: move |_| count.set(0),
  30. "Click me!"
  31. }
  32. }
  33. })
  34. }