dioxus/packages/web/examples/timeout_count.rs

35 lines
926 B
Rust
Raw Normal View History

2023-04-09 14:54:18 +00:00
// https://jakelazaroff.com/words/were-react-hooks-a-mistake/
use dioxus::prelude::*;
fn main() {
dioxus_web::launch(app);
}
fn app() -> Element {
2024-01-14 05:12:21 +00:00
let count = use_signal(|| 0);
let started = use_signal(|| false);
2023-04-09 14:54:18 +00:00
let start = move || {
if !*started.get() {
let count = count.clone(); // clone reference rather than value
let alert = move || gloo_dialogs::alert(&format!("Your score was {}!", count.read()));
gloo_timers::callback::Timeout::new(5_000, alert).forget();
}
started.set(true); // this cannot be done inside condition or infinite loop
};
2024-01-14 05:12:21 +00:00
rsx! {
2023-04-09 14:54:18 +00:00
button {
onclick: move |_event| {
start();
*count.write() += 1;
},
if **started {
"Current score: {count.read()}"
} else {
"Start"
}
2023-04-09 14:54:18 +00:00
}
2024-01-14 05:12:21 +00:00
}
2023-04-09 14:54:18 +00:00
}