feat: overhaul examples and clean things up
This commit is contained in:
1 parent
601632fc7e
commit
420a30e5d4
33 files changed
+1089
-1417
No files matched your search
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
This example shows how to use async and loops to implement a coroutine in a component. Coroutines can be controlled via
|
||||
the `TaskHandle` object.
|
||||
*/
|
||||
|
||||
use dioxus::prelude::*;
|
||||
use gloo_timers::future::TimeoutFuture;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
dioxus::desktop::launch(app);
|
||||
}
|
||||
|
||||
fn app(cx: Scope) -> Element {
|
||||
let count = use_state(&cx, || 0);
|
||||
let direction = use_state(&cx, || 1);
|
||||
|
||||
let (async_count, dir) = (count.for_async(), *direction);
|
||||
|
||||
let task = use_coroutine(&cx, move || {
|
||||
//
|
||||
async move {
|
||||
loop {
|
||||
TimeoutFuture::new(250).await;
|
||||
// *async_count.modify() += dir;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
cx.render(rsx! {
|
||||
div {
|
||||
h1 {"count is {count}"}
|
||||
button { onclick: move |_| task.stop(),
|
||||
"Stop counting"
|
||||
}
|
||||
button { onclick: move |_| task.resume(),
|
||||
"Start counting"
|
||||
}
|
||||
button {
|
||||
onclick: move |_| {
|
||||
*direction.modify() *= -1;
|
||||
task.restart();
|
||||
},
|
||||
"Switch counting direcion"
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user