1
0

error.rs 908 B

12345678910111213141516171819202122232425262728293031
  1. #[component]
  2. fn Counters() -> Element {
  3. let counts = use_signal(Vec::new);
  4. let mut children = use_signal(|| 0);
  5. rsx! {
  6. button { onclick: move |_| children += 1, "Add child" }
  7. button { onclick: move |_| children -= 1, "Remove child" }
  8. // A signal from a child is read or written to in a parent scope
  9. "{counts:?}"
  10. for _ in 0..children() {
  11. Counter {
  12. counts
  13. }
  14. }
  15. }
  16. }
  17. #[component]
  18. fn Counter(mut counts: Signal<Vec<Signal<i32>>>) -> Element {
  19. let mut signal_owned_by_child = use_signal(|| 0);
  20. // Moving the signal up to the parent may cause issues if you read the signal after the child scope is dropped
  21. use_hook(|| counts.push(signal_owned_by_child));
  22. rsx! {
  23. button {
  24. onclick: move |_| signal_owned_by_child += 1,
  25. "{signal_owned_by_child}"
  26. }
  27. }
  28. }