Function Suspense

pub fn Suspense<Chil>(props: SuspenseProps<Chil>) -> impl IntoView
where Chil: IntoView + Send + 'static,
Expand description

If any Resource is read in the children of this component, it will show the fallback while they are loading. Once all are resolved, it will render the children.

Each time one of the resources is loading again, it will fall back. To keep the current children instead, use Transition.

Note that the children will be rendered initially (in order to capture the fact that those resources are read under the suspense), so you cannot assume that resources read synchronously have Some value in children. However, you can read resources asynchronously by using Suspend.

async fn fetch_cats(how_many: u32) -> Vec<String> { vec![] }

let (cat_count, set_cat_count) = signal::<u32>(1);

let cats = Resource::new(move || cat_count.get(), |count| fetch_cats(count));

view! {
  <div>
    <Suspense fallback=move || view! { <p>"Loading (Suspense Fallback)..."</p> }>
      // you can access a resource synchronously
      {move || {
          cats.get().map(|data| {
            data
              .into_iter()
              .map(|src| {
                  view! {
                    <img src={src}/>
                  }
              })
              .collect_view()
          })
        }
      }
      // or you can use `Suspend` to read resources asynchronously
      {move || Suspend::new(async move {
        cats.await
              .into_iter()
              .map(|src| {
                  view! {
                    <img src={src}/>
                  }
              })
              .collect_view()
      })}
    </Suspense>
  </div>
}

§Required Props

  • children: TypedChildren<Chil>
    • Children will be rendered once initially to catch any resource reads, then hidden until all data have loaded.

§Optional Props

  • fallback: impl Into<ViewFnOnce>
    • A function that returns a fallback that will be shown while resources are still loading. By default this is an empty view.