mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-27 14:40:44 +00:00
42 lines
1.1 KiB
Rust
42 lines
1.1 KiB
Rust
//! Example: README.md showcase
|
|
//!
|
|
//! The example from the README.md
|
|
|
|
use dioxus::prelude::*;
|
|
fn main() {
|
|
dioxus::desktop::launch(App, |c| c).expect("faield to launch");
|
|
}
|
|
|
|
pub static App: FC<()> = |cx| {
|
|
let count = use_state(cx, || 0);
|
|
let mut direction = use_state(cx, || 1);
|
|
|
|
let (async_count, dir) = (count.for_async(), *direction);
|
|
let (task, _result) = use_task(cx, move || async move {
|
|
loop {
|
|
gloo_timers::future::TimeoutFuture::new(250).await;
|
|
*async_count.get_mut() += dir;
|
|
}
|
|
});
|
|
|
|
cx.render(rsx! {
|
|
div {
|
|
h1 {"count is {count}"}
|
|
button {
|
|
"Stop counting"
|
|
onclick: move |_| task.stop()
|
|
}
|
|
button {
|
|
"Start counting"
|
|
onclick: move |_| task.start()
|
|
}
|
|
button {
|
|
"Switch counting direcion"
|
|
onclick: move |_| {
|
|
direction *= -1;
|
|
task.restart();
|
|
}
|
|
}
|
|
}
|
|
})
|
|
};
|