tasks.rs 735 B

12345678910111213141516171819202122232425262728293031323334
  1. //! Example: README.md showcase
  2. //!
  3. //! The example from the README.md.
  4. use dioxus::prelude::*;
  5. use std::time::Duration;
  6. fn main() {
  7. dioxus::desktop::launch(app);
  8. }
  9. fn app(cx: Scope) -> Element {
  10. let (count, set_count) = use_state(&cx, || 0);
  11. use_future(&cx, move || {
  12. let set_count = set_count.to_owned();
  13. async move {
  14. loop {
  15. tokio::time::sleep(Duration::from_millis(1000)).await;
  16. set_count.modify(|f| f + 1);
  17. }
  18. }
  19. });
  20. cx.render(rsx! {
  21. div {
  22. h1 { "Current count: {count}" }
  23. button {
  24. onclick: move |_| set_count(0),
  25. "Reset the count"
  26. }
  27. }
  28. })
  29. }