Skip to Content
Effetch Query
React

Suspense and error boundaries

Let Suspense show loading and error boundaries show failures.

useSuspenseQuery suspends until the query has a value, then returns its Success, which stays waiting while it refetches. A failure with nothing to show is thrown to the nearest error boundary as a QueryFailure, which carries the query and its Cause.

The examples import todos.ts from Getting started.

QueryBoundary handles both: it shows pending while the queries inside suspend, and calls failure when one of them throws a QueryFailure. retry refetches the failed query and shows the children again:

import {
  ,
  ,
} from "@monarcode/effetch-query/react";

import {  } from "./todos.js";

function ({  }: { readonly : number }) {
  const {  } = (({ : {  } }));
  return <>{.}</>;
}

export function ({  }: { readonly : number }) {
  return (
    <
      ={}
      ={<>Loading…</>}
      ={({ ,  }) => (
        < ={}>{()}: try again</>
      )}
    >
      < ={} />
    </>
  );
}

Any other error goes on to the next error boundary up. A refetch that fails after a success keeps showing that success.

The boundary shows failure until retry is called, or until the failed query succeeds some other way, such as a refetch on reconnect. retry refetches the query that failed: give each part of a screen that can fail on its own a boundary of its own. To show other content after a failure, such as the next todo, give the boundary a key (key={id}), so React makes a new one.

A few things are particular to suspense readers:

  • A value under a second old isn't refetched on mount, as in TanStack Query, so the value a component suspended on isn't fetched again as it mounts. An older stale value is shown and refetched in the background.
  • A failure stays thrown until something refetches the query. A suspense reader never starts using the failed query, so retryOnMount doesn't retry it: retry or useResetQueryFailure does.
  • In a server render, it fetches a query the cache doesn't have, and suspends on the fetch, for streaming (Stream the cache).

failure gets the failure as error, squashed to one value for display, with its cause and retry. A QueryFailure has the same error and cause, with the query that failed. QueryFailure.is tells it from other errors, and it's exported by both @monarcode/effetch-query and /react. Code outside React never sees one: there, a query's failure keeps its own error type.

Where a router owns the error boundary, such as TanStack Router's errorComponent or a React Router ErrorBoundary export, call useResetQueryFailure there. Given the caught error, it refetches the query inside a QueryFailure, and ignores any other error; then reset the router's boundary:

import {
  ,
  ,
} from "@monarcode/effetch-query/react";
import type {  } from "@tanstack/react-router";

export function ({ ,  }: ) {
  const  = ();
  const  = .() ? . : ;
  return (
    <
      ={() => {
        ();
        ();
      }}
    >
      {()}: try again
    </>
  );
}