suspense.rs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //! Example: Suspense
  2. //! -----------------
  3. //! This example shows how the "use_fetch" hook is built on top of Dioxus' "suspense" API. Suspense enables components
  4. //! to wait on futures to complete before rendering the result into VNodes. These VNodes are immediately available in a
  5. //! "suspended" fashion and will automatically propogate to the UI when the future completes.
  6. //!
  7. //! Note that if your component updates or receives new props while it is awating the result, the future will be dropped
  8. //! and all work will stop. In this example, we store the future in a hook so we can always resume it.
  9. use dioxus::prelude::*;
  10. fn main() {}
  11. #[derive(serde::Deserialize)]
  12. struct DogApi {
  13. message: String,
  14. }
  15. const ENDPOINT: &str = "https://dog.ceo/api/breeds/image/random";
  16. pub static App: FC<()> = |cx| {
  17. let doggo = use_future_effect(&cx, move || async move {
  18. match surf::get(ENDPOINT).recv_json::<DogApi>().await {
  19. Ok(res) => rsx!(in cx, img { src: "{res.message}" }),
  20. Err(_) => rsx!(in cx, div { "No doggos for you :(" }),
  21. }
  22. });
  23. cx.render(rsx!(
  24. div {
  25. h1 {"Waiting for a doggo..."}
  26. {doggo}
  27. }
  28. ))
  29. };
  30. use dioxus_core::virtual_dom::SuspendedContext;
  31. use futures::Future;
  32. use futures::FutureExt;
  33. use std::pin::Pin;
  34. fn use_fetch<'a, T: serde::de::DeserializeOwned + 'static>(
  35. cx: &impl Scoped<'a>,
  36. url: &str,
  37. g: impl FnOnce(SuspendedContext, surf::Result<T>) -> VNode<'a> + 'a,
  38. ) -> VNode<'a> {
  39. // essentially we're doing a "use_effect" but with no dependent props or post-render shenanigans
  40. let fetch_promise = cx.use_hook(
  41. move || surf::get(url).recv_json::<T>().boxed_local(),
  42. // just pass the future through
  43. |p| p,
  44. |_| (),
  45. );
  46. cx.suspend(fetch_promise, g)
  47. }
  48. /// Spawns the future only when the inputs change
  49. fn use_future_effect<'a, 'b, F: Future<Output = VNode<'b>>>(
  50. cx: &impl Scoped<'a>,
  51. g: impl FnOnce() -> F + 'a,
  52. ) -> VNode<'a> {
  53. todo!()
  54. }