dioxus/examples/reference/suspense.rs

60 lines
2 KiB
Rust
Raw Normal View History

2021-06-26 01:15:33 +00:00
//! Example: Suspense
//! -----------------
//! This example shows how the "use_fetch" hook is built on top of Dioxus' "suspense" API. Suspense enables components
//! to wait on futures to complete before rendering the result into VNodes. These VNodes are immediately available in a
//! "suspended" fashion and will automatically propogate to the UI when the future completes.
//!
//! Note that if your component updates or receives new props while it is awating the result, the future will be dropped
//! and all work will stop. In this example, we store the future in a hook so we can always resume it.
use dioxus::prelude::*;
fn main() {}
#[derive(serde::Deserialize)]
struct DogApi {
message: String,
}
const ENDPOINT: &str = "https://dog.ceo/api/breeds/image/random";
pub static App: FC<()> = |cx| {
2021-06-26 05:30:20 +00:00
let doggo = use_future_effect(&cx, move || async move {
match surf::get(ENDPOINT).recv_json::<DogApi>().await {
Ok(res) => rsx!(in cx, img { src: "{res.message}" }),
Err(_) => rsx!(in cx, div { "No doggos for you :(" }),
}
2021-06-26 01:15:33 +00:00
});
cx.render(rsx!(
div {
h1 {"Waiting for a doggo..."}
{doggo}
}
))
};
2021-06-26 05:30:20 +00:00
use dioxus_core::virtual_dom::SuspendedContext;
use futures::Future;
use futures::FutureExt;
use std::pin::Pin;
2021-06-26 01:15:33 +00:00
fn use_fetch<'a, T: serde::de::DeserializeOwned + 'static>(
cx: &impl Scoped<'a>,
url: &str,
2021-06-26 05:30:20 +00:00
g: impl FnOnce(SuspendedContext, surf::Result<T>) -> VNode<'a> + 'a,
2021-06-26 01:15:33 +00:00
) -> VNode<'a> {
2021-06-26 05:30:20 +00:00
// essentially we're doing a "use_effect" but with no dependent props or post-render shenanigans
let fetch_promise = cx.use_hook(
move || surf::get(url).recv_json::<T>().boxed_local(),
// just pass the future through
|p| p,
|_| (),
);
cx.suspend(fetch_promise, g)
}
2021-06-26 01:15:33 +00:00
2021-06-26 05:30:20 +00:00
/// Spawns the future only when the inputs change
fn use_future_effect<'a, 'b, F: Future<Output = VNode<'b>>>(
cx: &impl Scoped<'a>,
g: impl FnOnce() -> F + 'a,
) -> VNode<'a> {
todo!()
2021-06-26 01:15:33 +00:00
}