Select part of a value
Subscribe a component to one part of a query's value.
select derives what a component shows from a query's value, such as a
count or one field. useQuery, useSuspenseQuery and useQueryState
take it:
The examples import todos.ts from Getting started.
import * as from "effect/reactivity/AsyncResult";
import { } from "@monarcode/effetch-query/react";
import { } from "./todos.js";
export function () {
const = ((), {
: () => .(() => !.).,
});
return <>{.(, () => 0)} open</>;
}The component re-renders when the selection changes or a fetch starts or
ends, not for changes the selection doesn't show: a selection equal to
the last one, compared by content (Equal.equals), keeps the result it
showed. An inline function runs on every render; for a costly selection,
define it outside the component or with useCallback.
The cache keeps the whole value, so other components reading the query
see it unchanged, and the query still refetches on mount, focus and
polling as without select. A failure's previous success is selected
too, and with keepPrevious, the last selection shown is kept.
A select that throws shows as a failure with the defect;
useSuspenseQuery throws it to the error boundary as a QueryFailure.
The options' types are UseQuerySelectOptions, for useQuery and
useQueryState, and UseSuspenseQueryOptions, for useSuspenseQuery.