Function freya::prelude::use_future
pub fn use_future<F>(future: impl FnMut() -> F + 'static) -> UseFuturewhere
F: Future + 'static,
Expand description
A hook that allows you to spawn a future.
This future will not run on the server
The future is spawned on the next call to wait_for_next_render
which means that it will not run on the server.
To run a future on the server, you should use spawn
directly.
use_future
won’t return a value.
If you want to return a value from a future, use use_resource
instead.
fn app() -> Element {
let mut count = use_signal(|| 0);
let mut running = use_signal(|| true);
// `use_future` will spawn an infinitely running future that can be started and stopped
use_future(move || async move {
loop {
if running() {
count += 1;
}
tokio::time::sleep(Duration::from_millis(400)).await;
}
});
rsx! {
div {
h1 { "Current count: {count}" }
button { onclick: move |_| running.toggle(), "Start/Stop the count"}
button { onclick: move |_| count.set(0), "Reset the count" }
}
}
}