dioxus/docs/platforms/04-concurrency.md

22 lines
916 B
Markdown
Raw Normal View History

2021-02-16 06:41:41 +00:00
## Concurrency
2021-06-26 01:15:33 +00:00
In Dioxus, VNodes are asynchronous and can their rendering can be paused at any time by awaiting a future. Hooks can combine this functionality with the Context and Subscription APIs to craft dynamic and efficient user experiences.
2021-02-16 06:41:41 +00:00
```rust
2021-07-18 16:39:32 +00:00
fn user_data(cx: Context<()>) -> DomTree {
2021-02-16 06:41:41 +00:00
// Register this future as a task
2021-06-26 01:15:33 +00:00
use_suspense(cx, async {
2021-02-16 06:41:41 +00:00
// Continue on with the component as usual, waiting for data to arrive
let Profile { name, birthday, .. } = fetch_data().await;
html! {
<div>
{"Hello, {name}!"}
{if birthday === std::Instant::now() {html! {"Happy birthday!"}}}
</div>
}
})
}
```
2021-06-26 01:15:33 +00:00
Asynchronous components are powerful but can also be easy to misuse as they pause rendering for the component and its children. Refer to the concurrent guide for information on how to best use async components.