mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-10 06:34:20 +00:00
1dfa1b5e7f
* Use of async_std::task::sleep instead of tokio::time::sleep * Make the clock example run on wasm * Add control_focus and eval examples to Cargo.toml * Use web-time on desktop; It just falls back to std on non-wasm platforms --------- Co-authored-by: Evan Almloff <evanalmloff@gmail.com>
38 lines
968 B
Rust
38 lines
968 B
Rust
//! A simple example that shows how to use the use_future hook to run a background task.
|
|
//!
|
|
//! use_future won't return a value, analogous to use_effect.
|
|
//! If you want to return a value from a future, use use_resource instead.
|
|
|
|
use async_std::task::sleep;
|
|
use dioxus::prelude::*;
|
|
|
|
fn main() {
|
|
launch(app);
|
|
}
|
|
|
|
fn app() -> Element {
|
|
let mut count = use_signal(|| 0);
|
|
|
|
// use_future will run the future
|
|
use_future(move || async move {
|
|
loop {
|
|
sleep(std::time::Duration::from_millis(200)).await;
|
|
count += 1;
|
|
}
|
|
});
|
|
|
|
// We can also spawn futures from effects, handlers, or other futures
|
|
use_effect(move || {
|
|
spawn(async move {
|
|
sleep(std::time::Duration::from_secs(5)).await;
|
|
count.set(100);
|
|
});
|
|
});
|
|
|
|
rsx! {
|
|
div {
|
|
h1 { "Current count: {count}" }
|
|
button { onclick: move |_| count.set(0), "Reset the count" }
|
|
}
|
|
}
|
|
}
|