dioxus/packages/desktop/examples/async.rs

39 lines
860 B
Rust
Raw Normal View History

2021-11-16 06:25:38 +00:00
use dioxus::prelude::*;
use dioxus_core as dioxus;
use dioxus_core_macro::*;
use dioxus_hooks::*;
use dioxus_html as dioxus_elements;
2021-12-29 04:20:01 +00:00
use std::time::Duration;
2021-11-16 06:25:38 +00:00
fn main() {
dioxus_desktop::launch(app);
2021-11-16 06:25:38 +00:00
}
fn app(cx: Scope<()>) -> Element {
2021-12-29 04:20:01 +00:00
let count = use_state(&cx, || 0);
2021-11-16 06:25:38 +00:00
2021-12-29 04:20:01 +00:00
// push the futureo on initialization
cx.use_hook(
|_| {
cx.push_future({
let count = count.for_async();
async move {
tokio::time::sleep(Duration::from_millis(1000)).await;
*count.get_mut() += 1;
}
});
},
|_| {},
);
2021-11-16 06:25:38 +00:00
cx.render(rsx! {
div {
h1 { "High-Five counter: {count}" }
button {
onclick: move |_| count.set(0),
"Click me!"
}
}
})
}