I am working on a simple application using the Leptos framework which updates a counter. I have cloned a ReadSignal
and its corresponding setter function using .clone()
. However, I noticed that when I update the original and the cloned signals, they both increment the same value.
#[component]
fn App(cx: Scope) -> impl IntoView {
let (count, set_count) = create_signal(cx, 0);
let dup_count = count.clone();
let dup_set = set_count.clone();
view! { cx,
<div>
<button
on:click=move |_| {
set_count.update(|n| *n += 1);
dup_set.update(|n| *n += 2);
}
>
"Click me: "
{count.get()}
</button>
<div>{count}</div>
<div>{dup_count}</div>
<ProgressBar progress=count />
<ProgressBar progress=dup_count />
</div>
}
}
I am trying to better understand Signals and the different methods in Leptos. I would love some explanations to deepen my understanding. Thank you in advance!
The solution to your problem might be adding move ||:
See https://leptos-rs.github.io/leptos/view/01_basic_component.html where this is explained.