fixed_list.rs 790 B

1234567891011121314151617181920212223242526272829303132
  1. #[component]
  2. fn Counters() -> Element {
  3. let mut counts = use_signal(Vec::new);
  4. rsx! {
  5. button { onclick: move |_| counts.write().push(0), "Add child" }
  6. button {
  7. onclick: move |_| {
  8. counts.write().pop();
  9. },
  10. "Remove child"
  11. }
  12. "{counts:?}"
  13. // Instead of passing up a signal, we can just write to the signal that lives in the parent
  14. for index in 0..counts.len() {
  15. Counter {
  16. index,
  17. counts
  18. }
  19. }
  20. }
  21. }
  22. #[component]
  23. fn Counter(index: usize, mut counts: Signal<Vec<i32>>) -> Element {
  24. rsx! {
  25. button {
  26. onclick: move |_| counts.write()[index] += 1,
  27. "{counts.read()[index]}"
  28. }
  29. }
  30. }