React
Read a query as an object
Read a query's data, error and loading flags as fields.
useQueryState reads a query as useQuery does, with the same options,
and returns TanStack Query's shape: status, data, error,
isFetching and refetch. Check status first; it narrows data and
error:
The examples import todos.ts from Getting started.
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 fields are:
status:"pending","success"or"failure", withisPending,isSuccessandisFailureto match. A query is pending before its first fetch settles, after aclearand when it'snull.data: the value. A failed refetch keeps the last value asdata.error: the typed error, orundefinedfor a defect, whichcauseholds with the rest of the failure.isFetching: true while a fetch runs, the first one or a refetch.isPlaceholder: true whiledatais a placeholder, or the valuekeepPreviouskeeps, rather than the query's own value (Show a placeholder). Use it to dim the data, or to turn off what needs the real value, such as editing it.refetch: refetches the query, and resolves when it settles. It never rejects, and keeps its identity while the query stays the same.result: theAsyncResultthe fields come from, forAsyncResult.builder.
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.
error is a union of every error the query can fail with. To handle each
by its _tag, match on useQuery's result
(Read a query) or on state.result.