backgrounded_futures.rs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. use dioxus::prelude::*;
  2. fn main() {
  3. launch_desktop(app);
  4. }
  5. fn app() -> Element {
  6. let mut show_child = use_signal(|| true);
  7. let mut count = use_signal(|| 0);
  8. let child = use_memo(move || {
  9. rsx! {
  10. Child {
  11. count
  12. }
  13. }
  14. });
  15. rsx! {
  16. button { onclick: move |_| show_child.toggle(), "Toggle child" }
  17. button { onclick: move |_| count += 1, "Increment count" }
  18. if show_child() {
  19. {child.cloned()}
  20. }
  21. }
  22. }
  23. #[component]
  24. fn Child(count: Signal<i32>) -> Element {
  25. let mut early_return = use_signal(|| false);
  26. let early = rsx! {
  27. button { onclick: move |_| early_return.toggle(), "Toggle {early_return} early return" }
  28. };
  29. if early_return() {
  30. return early;
  31. }
  32. use_future(move || async move {
  33. loop {
  34. tokio::time::sleep(std::time::Duration::from_millis(100)).await;
  35. println!("Child")
  36. }
  37. });
  38. use_effect(move || {
  39. println!("Child count: {}", count());
  40. });
  41. rsx! {
  42. "hellO!"
  43. {early}
  44. }
  45. }