Add toggle to signals example

This commit is contained in:
Jonathan Kelley 2023-10-15 17:50:23 -07:00
parent 14b3fa72e4
commit 741bfbc9ac
No known key found for this signature in database
GPG Key ID: 1FBB50F7EB0A08BE
1 changed files with 6 additions and 5 deletions

View File

@ -6,12 +6,15 @@ fn main() {
}
fn app(cx: Scope) -> Element {
let running = dioxus_signals::use_signal(cx, || true);
let mut count = dioxus_signals::use_signal(cx, || 0);
let saved_values = dioxus_signals::use_signal(cx, || vec![0]);
use_future!(cx, || async move {
loop {
count += 1;
if running.value() {
count += 1;
}
tokio::time::sleep(Duration::from_millis(400)).await;
}
});
@ -20,10 +23,8 @@ fn app(cx: Scope) -> Element {
h1 { "High-Five counter: {count}" }
button { onclick: move |_| count += 1, "Up high!" }
button { onclick: move |_| count -= 1, "Down low!" }
button {
onclick: move |_| saved_values.push(count.value()),
"Save this value"
}
button { onclick: move |_| running.set(!running.value()), "Toggle counter" }
button { onclick: move |_| saved_values.push(count.value()), "Save this value" }
// We can do boolean operations on the current signal value
if count.value() > 5 {