clock.rs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #![allow(non_snake_case)]
  2. use dioxus::prelude::*;
  3. use dioxus_signals::{use_signal, Effect, Signal};
  4. fn main() {
  5. dioxus_desktop::launch(app);
  6. }
  7. fn app(cx: Scope) -> Element {
  8. println!("running app");
  9. let counts = use_signal(cx, || (0..100).map(Signal::new).collect::<Vec<_>>());
  10. cx.use_hook(|| {
  11. Effect::new(move || {
  12. println!("Counts: {:?}", counts);
  13. })
  14. });
  15. render! {
  16. for (i, count) in counts.into_iter().enumerate() {
  17. Child {
  18. id: i,
  19. count: count,
  20. }
  21. }
  22. }
  23. }
  24. #[derive(Props, PartialEq)]
  25. struct ChildProps {
  26. id: usize,
  27. count: Signal<u64>,
  28. }
  29. fn Child(cx: Scope<ChildProps>) -> Element {
  30. println!("running child {}", cx.props.id);
  31. let count = cx.props.count;
  32. use_future!(cx, || async move {
  33. loop {
  34. tokio::time::sleep(std::time::Duration::from_secs(count.value())).await;
  35. *count.write() += 1;
  36. }
  37. });
  38. render! {
  39. div {
  40. "Child: {count}"
  41. button {
  42. onclick: move |_| {
  43. *count.write() += 1;
  44. },
  45. "Increase"
  46. }
  47. button {
  48. onclick: move |_| {
  49. *count.write() -= 1;
  50. },
  51. "Decrease"
  52. }
  53. }
  54. }
  55. }