Hi, I've two questions on the following minimal example where I want to invoke a callback for each item a Vec signal.
-
As soon as I use
*ein the callback, I get a compile error'elems' does not live long enoughon the loop. I can resolve this by doingfor e in elems()instead but do I understand correctly that this would cause a clone the vec on each render? The other way around this is to make all callbacks take the loop index instead – is that the idiomatic approach? -
I need to make
removemutto be able to use it in the loop. I don't really understand why as the lambda itself should never change? In the example here it is not an issue yet, but it becomes one once I want to nest lambdas likeonclick: move |_| run_and_log("rm elem", || remove_cell(i))as the capturedremove_cellbecomes non-mutable again. Is there a better way to do this?
let mut elems = use_signal(|| vec![100, 101, 102]);
let mut remove = move |id| {
elems.write().retain(|&x| x != id);
};
rsx! {
for e in elems.iter() {
div {
"elem {e}"
button {
onclick: move |_| remove(*e),
"remove"
}
}
}
}