Read a query as an object
Read a query's data, error and loading flags as fields.
If you'd rather check fields than match on an AsyncResult, as you would
with TanStack Query's useQuery, read the query with useQueryState. It
fetches and takes options as useQuery does, select, keepPrevious and
paused included, and returns status, data, error, isFetching,
refetch and a few more fields. This page shows how to read them, where
they differ from TanStack Query's, and the hooks that return the same
object for Suspense, infinite queries and lists of queries.
The examples import todos.ts from Getting started.
Check the status, then read the fields
status narrows data and error, so check it first:
import { } from "@monarcode/effetch-query/react";
import { } from "./todos.js";
export function ({ }: { readonly : number }) {
const = (({ : { } }));
if (. === "pending") {
return <>Loading…</>;
}
if (. === "failure") {
return (
< ={() => void .()}>
Couldn't load the todo: try again
</>
);
}
return (
< ={{ : . ? 0.5 : 1 }}>{..}</>
);
}The object's type is UseQueryStateResult, a union of three shapes told
apart by status. The object keeps its identity while nothing in it
changes.
The fields
useQueryState returns these fields:
status:"pending","success"or"failure", withisPending,isSuccessandisFailureto match. A query is pending before its first fetch settles, after aclear, and when it'snull.data: the value, or its selection withselect. A failed refetch keeps the last value asdata.error: the typed error. It'sundefinedwhen the query died with a defect or was interrupted.cause: the failure's fullCause, which holds the error, the defect or the interruption.isFetching: true while a fetch runs, the first one or a refetch.isPlaceholder: true whiledataisn't the query's own value yet: see Tell a placeholder from the real value.refetch: refetches the query, and resolves when the refetch settles. It never rejects, and keeps its identity while the query stays the same.result: theAsyncResultthe fields come from.
error is a union of every error the query can fail with. To handle each
by its _tag, match on state.result, or on useQuery's result: see
Read a query.
- In a server render, a query the cache doesn't have reads as fetching, though nothing fetches it there.
- A
pausedreader fetches nothing, soisFetchingstays false for it. refetchresolves at once for anullquery, and for a query that only paused readers use: see Pause screens you can't see.
Differences from TanStack Query
The fields are close to TanStack Query's, not the same. Coming from it, these are the differences:
statusis"failure", not"error", and its flag isisFailure, notisError.isPlaceholderreplacesisPlaceholderData.refetchresolves with nothing, not with the new result, and never rejects: a failure shows in the fields.errorisundefinedfor a defect or an interruption. Readcausefor those.- There's no
isLoading,isRefetchingorisStale. For a refetch you started yourself, such as pull to refresh, useuseRefetch'sisRefetching: see Refetch on demand.
Tell a placeholder from the real value
Some values a component shows aren't the query's own yet: a
placeholder, such as the list's copy of a
todo, or the previous todo that keepPrevious keeps while the next one
loads. isPlaceholder is true for both. Use it to dim the data, or to turn
off what needs the real value, such as editing it:
import { } from "@monarcode/effetch-query/react";
import { } from "./todos.js";
export function ({ }: { readonly : number }) {
const = (({ : { } }), {
: true,
});
if (. !== "success") {
return null;
}
// Editing waits for this todo, not the one kept from before.
return (
< ={..} ={.} />
);
}isPlaceholder stays true until the query's own value or failure replaces
the placeholder, and isFetching is true meanwhile. A failure's data is
always the query's own last value, so isPlaceholder is false there.
A paused reader fetches nothing, so it keeps showing a placeholder, with
isPlaceholder true and isFetching false, until it resumes.
Suspense treats a placeholder as a value. In the browser,
useSuspenseQuery doesn't suspend on one: it returns it as a waiting
Success. A server render doesn't show a placeholder, so there, and while
the page hydrates, useSuspenseQuery suspends until the query's own value
arrives.
Read with Suspense
useSuspenseQueryState reads a query as
useSuspenseQuery does, and returns the same
fields. It suspends until the query has a value, so data is always
there:
import { } from "@monarcode/effetch-query/react";
import { } from "./todos.js";
export function ({ }: { readonly : number }) {
const = (({ : { } }));
return (
<>
{..}
{. ? (
< ={() => void .()}>Couldn't refresh</>
) : null}
</>
);
}status is "success" or "failure", never "pending". A failure with
no value before it goes to the error boundary, as with useSuspenseQuery.
A refetch that fails after a success doesn't: useSuspenseQuery keeps
showing the success, and useSuspenseQueryState shows the failure, with
the earlier value as data and the typed error beside it. Its only
option is select.
Read pages
useInfiniteQueryState and useSuspenseInfiniteQueryState read an
infinite query as useInfiniteQuery and
useSuspenseInfiniteQuery do. They return the fields, with data.pages
for the pages, beside the page controls:
import { } from "@monarcode/effetch-query/react";
import { } from "./activity.js";
export function () {
const = (({ : { : "web" } }));
if (. === "pending") {
return <>Loading…</>;
}
return (
<>
<>
{.?.
.(() => .)
.(() => (
< ={.}>{.}</>
))}
</>
{. ? (
<
={.}
={() => void .()}
>
Load more
</>
) : null}
</>
);
}A page that fails makes status "failure", with the pages loaded before
it still in data. A failure on the first page has no pages, so data is
undefined there. useSuspenseInfiniteQueryState throws that one to the
error boundary instead, so its data is always there.
Read several queries
useQueriesState reads a list of queries as
useQueries does, and returns an
object for each, in the same order:
import { } from "@monarcode/effetch-query/react";
import { } from "./todos.js";
export function ({
,
}: {
readonly : <number>;
}) {
const = (.(() => ({ : { } })));
return (
<>
{.((, ) => (
< ={[]}>
{. === "success" ? .. : "Loading…"}
</>
))}
</>
);
}Each object keeps its identity while its query's result does, and its
refetch while the query stays in the list. A null entry is pending,
and its refetch resolves at once. It takes paused, as useQueries
does.
What's next
- Show a placeholder: give a query a value to show while it loads.
- Read a query: match on the
AsyncResultitself, select part of a value, or read several queries. - Refetch on demand: refetch from a button, with a flag for that refetch only.
- Coming from TanStack Query: the rest of TanStack Query's names, mapped to Effetch Query's.