Add a warning when a copy value is used in a higher scope than it was created in (#2771)

This commit is contained in:
Evan Almloff authored and GitHub committed 2024-08-02 10:48:13 -07:00
1 parent 0acfe37398
commit 14081e7e45
9 files changed
+289 -115

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
#[component]
fn Counters() -> Element {
let counts = use_signal(Vec::new);
let mut children = use_signal(|| 0);
rsx! {
button { onclick: move |_| children += 1, "Add child" }
button { onclick: move |_| children -= 1, "Remove child" }
// A signal from a child is read or written to in a parent scope
"{counts:?}"
for _ in 0..children() {
Counter {
counts
}
}
}
}
#[component]
fn Counter(mut counts: Signal<Vec<Signal<i32>>>) -> Element {
let mut signal_owned_by_child = use_signal(|| 0);
// Moving the signal up to the parent may cause issues if you read the signal after the child scope is dropped
use_hook(|| counts.push(signal_owned_by_child));
rsx! {
button {
onclick: move |_| signal_owned_by_child += 1,
"{signal_owned_by_child}"
}
}
}