1
0

counters.rs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //! A simple counters example that stores a list of items in a vec and then iterates over them.
  2. use dioxus::prelude::*;
  3. fn main() {
  4. launch(app);
  5. }
  6. fn app() -> Element {
  7. // Store the counters in a signal
  8. let mut counters = use_signal(|| vec![0, 0, 0]);
  9. // Whenver the counters change, sum them up
  10. let sum = use_memo(move || counters.read().iter().copied().sum::<i32>());
  11. rsx! {
  12. style { {include_str!("./assets/counter.css")} }
  13. div { id: "controls",
  14. button { onclick: move |_| counters.write().push(0), "Add counter" }
  15. button { onclick: move |_| { counters.write().pop(); }, "Remove counter" }
  16. }
  17. h3 { "Total: {sum}" }
  18. // Calling `iter` on a Signal<Vec<>> gives you a GenerationalRef to each entry in the vec
  19. // We enumerate to get the idx of each counter, which we use later to modify the vec
  20. for (i, counter) in counters.iter().enumerate() {
  21. // We need a key to uniquely identify each counter. You really shouldn't be using the index, so we're using
  22. // the counter value itself.
  23. //
  24. // If we used the index, and a counter is removed, dioxus would need to re-write the contents of all following
  25. // counters instead of simply removing the one that was removed
  26. //
  27. // You should use a stable identifier for the key, like a unique id or the value of the counter itself
  28. li { key: "{i}",
  29. button { onclick: move |_| counters.write()[i] -= 1, "-1" }
  30. input {
  31. r#type: "number",
  32. value: "{counter}",
  33. oninput: move |e| {
  34. if let Ok(value) = e.parsed() {
  35. counters.write()[i] = value;
  36. }
  37. }
  38. }
  39. button { onclick: move |_| counters.write()[i] += 1, "+1" }
  40. button { onclick: move |_| { counters.write().remove(i); }, "x" }
  41. }
  42. }
  43. }
  44. }