Suspense and error boundaries
Let Suspense show loading and error boundaries show failures.
If you'd rather not render a loading state and an error state in every
component, read the query with useSuspenseQuery. The component renders
only when there's a value: a Suspense boundary shows loading, and an
error boundary shows the failure. This page shows how to set up both with
QueryBoundary, and how to use a router's error boundary instead.
The examples import todos.ts from Getting started.
Read with useSuspenseQuery
useSuspenseQuery suspends until the query has a value, then returns its
Success. QueryBoundary handles both sides around it: it shows
pending while the queries inside suspend, and calls failure when one of
them fails:
import {
,
,
} from "@monarcode/effetch-query/react";
import { } from "./todos.js";
function ({ }: { readonly : number }) {
const { } = (({ : { } }));
return <>{.}</>;
}
export function ({ }: { readonly : number }) {
return (
<
={}
={<>Loading…</>}
={({ , }) => (
< ={}>{()}: try again</>
)}
>
< ={} />
</>
);
}The Success stays waiting while the query refetches, and a refetch
that fails after a success keeps showing that success. Only a failure with
nothing to show reaches the boundary: the hook throws it as a
QueryFailure, which carries the query and its Cause.
useSuspenseQuery takes a query, never null, and its only option is
select: see Select part of a value.
It has no keepPrevious and no paused. To wait for an argument, render
the component only when you have it. For an infinite query, use
useSuspenseInfiniteQuery: see Infinite queries.
To read data, error and status as fields instead, use
useSuspenseQueryState: see
Read with Suspense.
Show the failure and retry
failure gets the failure as error, squashed to one value for display,
with its cause and retry. retry refetches the query that failed and
shows the children again.
The boundary shows failure until retry is called, or until the failed
query succeeds some other way, such as a refetch on reconnect. Because
retry refetches only 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.
QueryBoundary catches only a QueryFailure. Any other error goes on to
the next error boundary up.
A QueryFailure has the same error and cause as failure gets, with
the query that failed. QueryFailure.is tells it from other errors, and
both @monarcode/effetch-query and @monarcode/effetch-query/react export
it. Code outside React never sees one: there, a query's failure keeps its
own error type.
How suspense readers differ
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
retryOnMountdoesn't retry it:retryoruseResetQueryFailuredoes. - A
selectthat throws is thrown to the error boundary as aQueryFailure, even after an earlier selection was shown. - A placeholder counts as a value in the browser, so the hook returns it rather than suspending: see Tell a placeholder from the real value.
- In a server render, it fetches a query the cache doesn't have, and
suspends on the fetch.
useQuerydoesn't. This is what streaming a server render relies on: see Queries started while the server renders.
Use a router's error boundary
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
</>
);
}What's next
- Stream the cache: stream a server render, with suspense readers fetching on the server.
- Frameworks: wire the error boundary of TanStack Start, Next.js or React Router.
- Infinite queries: load more pages, with or without suspense.