Add a warning when a copy value is used in a higher scope than it was created in (#2771)
This commit is contained in:
1 parent
0acfe37398
commit
14081e7e45
9 files changed
+289
-115
No files matched your search
@@ -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}"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#[component]
|
||||
fn Counters() -> Element {
|
||||
let mut counts = use_signal(Vec::new);
|
||||
|
||||
rsx! {
|
||||
button { onclick: move |_| counts.write().push(0), "Add child" }
|
||||
button {
|
||||
onclick: move |_| {
|
||||
counts.write().pop();
|
||||
},
|
||||
"Remove child"
|
||||
}
|
||||
"{counts:?}"
|
||||
// Instead of passing up a signal, we can just write to the signal that lives in the parent
|
||||
for index in 0..counts.len() {
|
||||
Counter {
|
||||
index,
|
||||
counts
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn Counter(index: usize, mut counts: Signal<Vec<i32>>) -> Element {
|
||||
rsx! {
|
||||
button {
|
||||
onclick: move |_| counts.write()[index] += 1,
|
||||
"{counts.read()[index]}"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user