freya::_docs

Module async_tasks

Source
Expand description

§Async

You may run asynchronous code through the different APIs Dioxus provide.

Using third-party libraries such as tokio to spawn tasks could work but it’s not recommended, these will not work with the lifecycling of the components.

§spawn

With spawn you can spawn an async task (Also known as green threads), this is primarily targeted for custom hooks or when you want to run some async code dynamically such as from an event listener.

Important: Tasks spawned with spawn will be cancelled when the component their were created is dropped. If you want to have an async tasks not attached to the component you may use spawn_forever.

fn app() -> Element {
    rsx!(Button {
        onclick: |_| {
            if 1 == 1 {
                spawn(async move {
                    println!("Hello, World fom an async task!");
                });
            }
        }
    })
}

You can also use hooks like use_future or use_resource.