2021-06-26 01:15:33 +00:00
|
|
|
//! Example: Suspense
|
|
|
|
//! -----------------
|
2021-07-16 20:11:25 +00:00
|
|
|
//! This example demonstrates how the use_suspense hook can be used to load and render asynchronous data. Suspense enables
|
|
|
|
//! components to wait on futures to complete before rendering the result into VNodes. These VNodes are immediately
|
2021-10-24 17:30:36 +00:00
|
|
|
//! available in a suspended" fashion and will automatically propagate to the UI when the future completes.
|
2021-06-26 01:15:33 +00:00
|
|
|
//!
|
2021-07-16 20:11:25 +00:00
|
|
|
//! Currently, suspense futures are non-restartable. In the future, we'll provide more granular control of how to start,
|
|
|
|
//! stop, and reset these futures.
|
2021-06-26 01:15:33 +00:00
|
|
|
|
|
|
|
use dioxus::prelude::*;
|
|
|
|
#[derive(serde::Deserialize)]
|
|
|
|
struct DogApi {
|
|
|
|
message: String,
|
|
|
|
}
|
|
|
|
const ENDPOINT: &str = "https://dog.ceo/api/breeds/image/random";
|
|
|
|
|
2022-01-02 23:35:38 +00:00
|
|
|
pub fn Example(cx: Scope) -> Element {
|
2021-07-27 15:28:05 +00:00
|
|
|
let doggo = use_suspense(
|
|
|
|
cx,
|
2021-07-16 20:11:25 +00:00
|
|
|
|| surf::get(ENDPOINT).recv_json::<DogApi>(),
|
|
|
|
|cx, res| match res {
|
2021-09-21 17:42:52 +00:00
|
|
|
Ok(res) => rsx!(
|
|
|
|
cx,
|
|
|
|
img {
|
|
|
|
src: "{res.message}"
|
|
|
|
}
|
|
|
|
),
|
2021-09-16 17:20:04 +00:00
|
|
|
Err(_) => rsx!(cx, div { "No doggos for you :(" }),
|
2021-07-16 20:11:25 +00:00
|
|
|
},
|
|
|
|
);
|
2021-06-26 01:15:33 +00:00
|
|
|
|
|
|
|
cx.render(rsx!(
|
|
|
|
div {
|
|
|
|
h1 {"Waiting for a doggo..."}
|
|
|
|
{doggo}
|
|
|
|
}
|
|
|
|
))
|
2022-01-02 23:35:38 +00:00
|
|
|
}
|