@monarcode/effetch-query/react
Every export of the React entry point: the provider, the hooks and the boundaries.
@monarcode/effetch-query/react exports the provider, the hooks and the boundaries. Import what you use:
import {
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
} from "@monarcode/effetch-query/react";Its exports follow, grouped by the guide that covers them, each with its docs. Your editor shows the same docs, with an example, when you hover the name.
Provide the client
Guide: 4. Show a query.
QueryProvider
Makes a QueryClient.QueryClient available to the hooks below it. It also provides the client's registry as @effect/atom-react's RegistryContext, so atom-react's own hooks (useAtomValue and the rest) share the cache. Read the client with useQueryClient. The hooks of @monarcode/effetch-query/react, and HydrationBoundary, throw a MissingQueryProviderError outside one.
Give it a client you own (QueryProviderProps), or a make it calls to own one, made again for each resetKey (OwnedQueryProviderProps): a sign-out or an account switch then drops the previous account's cache. A mounted provider can switch between the two forms: the owned client is disposed when a client replaces it.
QueryProviderProps
Props for QueryProvider with a client you own: the provider uses it and never disposes it. For a client the provider makes and disposes, see OwnedQueryProviderProps.
Prop
Type
Sign out and switch accounts
Guide: Sign out and switch accounts.
OwnedQueryProviderProps
Props for QueryProvider with a client the provider owns: it makes one with make, a new one each time resetKey changes, and disposes each when it's replaced or the provider unmounts. Pass the signed-in account's ID as resetKey, so no account sees another's cache.
For the browser and React Native only. Effects don't run in server rendering, so a client made there would never be disposed: on the server, give QueryProviderProps.client a client per request, and dispose it when the response is done (QueryClient.disposeAfter). Put it above every Suspense boundary: a first render React discards before it shows anything leaves the client it made undisposed.
Prop
Type
Read a query
Guide: Read a query.
MissingQueryProviderError
Thrown by useQueryClient, the other hooks of @monarcode/effetch-query/react and HydrationBoundary outside a QueryProvider.
useQuery
Reads a query and keeps it up to date while the component is mounted. On mount, and when query changes, it refetches the query if it's stale (refetchOnMount) or failed (retryOnMount), the mount policy, and while mounted it refetches on focus, reconnect and refetchInterval as the query's options say.
With select, it returns the result of the selection instead (UseQuerySelectOptions.select), and the component doesn't re-render for changes the selection doesn't show.
Pass null to read nothing: it returns AsyncResult.initial() and fetches nothing (TanStack's enabled: false). In a server render it reads only the cache and never starts a fetch, and the mount refetch doesn't run. Throws a MissingQueryProviderError outside a QueryProvider.
useQueryClient
Returns the QueryClient.QueryClient of the nearest QueryProvider, for running Effects from event handlers. Throws a MissingQueryProviderError outside one.
UseQueryOptions
Options for useInfiniteQuery, and the base of UseQuerySelectOptions, which useQuery and useQueryState take.
Prop
Type
Select part of a value
Guide: Select part of a value.
UseQuerySelectOptions
Options for useQuery and useQueryState: those of UseQueryOptions, and select.
Prop
Type
Read a query as an object
Guide: Read a query as an object.
useQueryState
Reads a query as useQuery does, and returns its result as an object of fields: status, data, error, isFetching, isPlaceholder, refetch and more, close to TanStack Query's shape. It fetches, refetches and takes options as useQuery does, select and keepPrevious included.
Check status before reading data: it narrows the object (UseQueryStateResult). A failure keeps the last success as data, and its error is typed; match each error by its _tag, or use result with AsyncResult.builder. The object keeps its identity while nothing in it changes. Throws a MissingQueryProviderError outside a QueryProvider.
UseQueryStateResult
What useQueryState returns: one of three shapes, told apart by status (or isPending, isSuccess and isFailure), which narrows data, error and cause.
useInfiniteQueryState
Reads an InfiniteQuery.InfiniteQuery as useInfiniteQuery does, and returns its result as an object of fields, as useQueryState does, with the page controls beside them: close to TanStack Query's useInfiniteQuery. data.pages holds the loaded pages once status is "success", and a page that fails makes it a "failure" whose data still holds them. The object keeps its identity while nothing in it changes. It fetches, and throws, as useInfiniteQuery does.
useQueriesState
Reads a list of queries as useQueries does, and returns each query's result as an object of fields, as useQueryState does: close to TanStack Query's useQueries. A null entry is pending, and its refetch resolves at once.
Each object keeps its identity while its query's result does, and its refetch while the query stays in the list. The array keeps its identity while every object in it does. A query listed twice shows the same object in both places. It fetches, pauses and throws as useQueries does.
useSuspenseInfiniteQueryState
Reads an InfiniteQuery.InfiniteQuery through React Suspense, as useSuspenseInfiniteQuery does, and returns its result as an object of fields, as useSuspenseQueryState does, with the page controls beside them: close to TanStack Query's useSuspenseInfiniteQuery. data.pages is always there. A page that fails makes status "failure", with the loaded pages still in data. The object keeps its identity while nothing in it changes. It fetches, and throws, as useSuspenseInfiniteQuery does.
useSuspenseQueryState
Reads a query through React Suspense, as useSuspenseQuery does, and returns its result as an object of fields, as useQueryState does: close to TanStack Query's useSuspenseQuery. It suspends until the query has a value and throws a failure with no value before it to the error boundary, so data is always there.
Unlike useSuspenseQuery, which keeps showing the earlier success after a refetch fails, the object says so: its status is "failure", with the typed error beside the earlier data. It fetches and takes select as useSuspenseQuery does. The object keeps its identity while nothing in it changes. Throws a MissingQueryProviderError outside a QueryProvider.
UseInfiniteQueryStateResult
What useInfiniteQueryState returns: useQueryState's object for the pages, narrowed by status, with the page controls beside it.
UseQueriesStateEntry
What useQueriesState returns for one entry of its list: the query's state object, typed by the query, or a pending one for null.
UseQueriesStateResult
What useQueriesState returns: a state object per query, in the same order, as a tuple for a tuple of queries and an array for an array.
UseQueryStateFailure
What useQueryState returns when the query has failed: its last fetch failed, or a refetch after a success did. A select that throws gives one too.
Prop
Type
UseQueryStatePending
What useQueryState returns while the query has no value and no failure: before its first fetch settles, after a clear, and for a null query.
Prop
Type
UseQueryStateSuccess
What useQueryState returns while the query has a value, including while it refetches.
Prop
Type
UseSuspenseInfiniteQueryStateResult
What useSuspenseInfiniteQueryState returns: useSuspenseQueryState's object for the pages, with the page controls beside it.
UseSuspenseQueryStateFailure
What useSuspenseQueryState returns after a refetch fails: the failure, with the value shown before it as data, since a suspense reader only throws a failure that has no value before it.
Prop
Type
UseSuspenseQueryStateResult
What useSuspenseQueryState returns: the success, or a failed refetch with the earlier value. data is there either way, so it needs no check; status tells the two apart.
Read many queries
Guide: Read several queries.
useQueries
Reads a list of queries whose length can change between renders, such as one per selected item, and keeps each up to date as useQuery does: it returns their results in the same order, and Initial for a null entry. Each query gets the mount policy when it joins the list. A query listed twice is read once, and shows in both places.
The component re-renders when one of the results changes. The array keeps its identity while every result in it does, so it can be a dependency of useMemo. Combine the results with AsyncResult.all. In a server render it reads only the cache and never starts a fetch. Throws a MissingQueryProviderError outside a QueryProvider.
UseQueriesEntry
What useQueries returns for one entry of its list: the query's result, typed by the query, or AsyncResult<never, never> for null. An entry that may be one of several queries is a union of their results.
UseQueriesOptions
Options for useQueries.
Prop
Type
UseQueriesResult
What useQueries returns: a result per query, in the same order, as a tuple for a tuple of queries and an array for an array.
Suspense and error boundaries
Guide: Suspense and error boundaries.
QueryBoundary
Shows pending while the queries below suspend, and failure when one of them fails with nothing to show: a Suspense boundary inside an error boundary that catches only QueryFailures. Its retry refetches the failed query and renders the children again. Any other error goes on to the next error boundary up.
It shows failure until retry is called or the failed query succeeds some other way, such as a refetch on reconnect: while it shows, the query stays in use. retry refetches only the query that failed, so 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 item, give it a key (key={id}), so React makes a new boundary.
Use it around components that read with useSuspenseQuery or useSuspenseInfiniteQuery. Where a router owns the error boundary (an errorComponent or an ErrorBoundary route export), use useResetQueryFailure there instead. In a server render it shows pending for what suspends, as Suspense does. Throws a MissingQueryProviderError outside a QueryProvider.
QueryFailure
Thrown by useSuspenseQuery and useSuspenseInfiniteQuery (from @monarcode/effetch-query/react) to the nearest error boundary when a query fails with no earlier value to show (a failed refetch keeps showing the last success instead), or when a reader's select throws. It carries the query, so the boundary can refetch it with useResetQueryFailure, and the full Cause. Tell it from other errors with QueryFailure.is. Code outside React never sees it: there, a query's failure keeps its own error type.
useResetQueryFailure
Returns a function for an error boundary: given what the boundary caught, it refetches the query inside a QueryFailure, so the boundary can render its children again and have them suspend on the refetch. Other errors are ignored. useSuspenseQuery and useSuspenseInfiniteQuery throw the failures it resets. Throws a MissingQueryProviderError outside a QueryProvider.
useSuspenseQuery
Reads a query through React Suspense: it suspends until the query has a value, then returns its Success, which stays waiting while it refetches. A stale value is shown and refetched in the background (refetchOnMount), but one under a second old isn't, as TanStack does, so the value a component suspended on isn't fetched again as it mounts. While suspended, a component keeps its query in use until the fetch settles, even if React abandons the render.
A failure with no earlier value is thrown to the nearest error boundary as a QueryFailure, as it was cached: it's thrown again until something refetches the query, such as useResetQueryFailure in the boundary. A refetch that fails after a success keeps showing that success.
With select, it returns the success of the selection instead (UseSuspenseQueryOptions.select), and the component doesn't re-render for changes the selection doesn't show.
In a server render, unlike useQuery, it fetches a query the cache doesn't have and suspends on the fetch, for streaming SSR. Throws a MissingQueryProviderError outside a QueryProvider.
QueryBoundaryFailureProps
What QueryBoundaryProps.failure is called with: the failure a query below the boundary threw, and a way to try again.
Prop
Type
QueryBoundaryProps
Props for QueryBoundary.
Prop
Type
UseSuspenseQueryOptions
Options for useSuspenseQuery.
Prop
Type
Refetch on demand
Guide: Refetch on demand.
useRefetch
Refetches queries on demand, such as for pull to refresh, with a flag that is true only while this component's own refetch is in progress, unlike a query's waiting, which a background refetch also sets. It refetches the Query.Target's queries in use, as Query.refetch does. For a screen made of queries with different keys, pass a Query.group of them: it's refetching until every one has settled. Throws a MissingQueryProviderError outside a QueryProvider, and a Query.KeyHoldsTargetError for a key that holds a query, a family or a group, such as [todos, profile]: pass a Query.group.
UseRefetchResult
What useRefetch returns.
Prop
Type
Load more
Guide: Load more.
useInfiniteQuery
Reads an InfiniteQuery.InfiniteQuery as useQuery does (the mount policy, focus, reconnect and polling refetch every loaded page), with controls to fetch a page at either end: "load more" buttons and infinite scroll.
A page fetch keeps the loaded pages shown: result stays a waiting Success, and isFetchingNextPage tells it from a refetch of every page. A page that fails makes result a Failure whose previousSuccess still holds the pages. Pass null to read nothing. It fetches, and throws, when useQuery does.
useSuspenseInfiniteQuery
Reads an InfiniteQuery.InfiniteQuery through React Suspense, as useSuspenseQuery does, with the same page controls as useInfiniteQuery. It suspends only until the first page: later pages load while the loaded ones show, through isFetchingNextPage. A page that fails isn't thrown, since there are pages to show; only a failure with no pages before it reaches the error boundary, as a QueryFailure. It fetches, and throws, when useSuspenseQuery does.
InfinitePageControls
The page controls useInfiniteQuery and useSuspenseInfiniteQuery return beside the result.
Prop
Type
UseInfiniteQueryResult
What useInfiniteQuery returns.
Prop
Type
UseSuspenseInfiniteQueryResult
What useSuspenseInfiniteQuery returns.
Prop
Type
Run a mutation
Guide: Run a mutation.
useMutation
Uses a mutation in this component, with state of its own: two components using one mutation never share a waiting or failed state. A run carries on after the component unmounts, so its onSuccess and invalidates still happen, though the callbacks given to run don't (UseMutationRunOptions). The mutation's concurrent decides how this component's runs overlap: false interrupts the previous one, "queue" runs them one at a time, and "drop" ignores a run while the one result shows is in progress. To show runs any component started, use useMutationState.
Define mutations at module level, with Mutation.make: a new definition on each render starts a new state. Outside React, run one with Mutation.run. Throws a MissingQueryProviderError outside a QueryProvider.
UseMutationResult
What useMutation returns.
Prop
Type
UseMutationRunOptions
Callbacks for one run of useMutation, which its run takes: TanStack's mutate(variables, { onSuccess, onError, onSettled }). They're called after the mutation's own callbacks, invalidates and the client's hooks, before code awaiting run carries on.
They're dropped if, by the time the run settles, reset was called, a newer run started in the component (with concurrent: "queue", one queued after it), or the component unmounted; and for a run that was interrupted, or that never started: dropped by concurrent: "drop", or on a disposed client. The run itself completes all the same. Use them for what matters only to this screen, such as filling in a form field or navigating, and the mutation's own onSuccess for what must always happen.
A callback that throws is logged with console.error; the ones after it still run, and run still resolves.
Prop
Type
Show activity
Guide: Show activity.
useIsFetching
Counts the queries with a fetch in progress on this client: those the Query.Target matches, or all of them without one, as Query.invalidate matches them. Background refetches count, so it suits an app-wide progress bar. The component re-renders when the count changes.
A server render counts none, and so does the first render while the page hydrates. Throws a MissingQueryProviderError outside a QueryProvider, and a Query.KeyHoldsTargetError for a key that holds a query, a family or a group.
useIsMutating
Counts the mutation runs in progress on this client: those of mutation, or of every mutation without one, whichever component or Effect started them. A run counts from when it starts until it settles, and a run concurrent: "queue" holds back counts while it waits. The component re-renders when the count changes.
A server render counts none, and so does the first render while the page hydrates. Throws a MissingQueryProviderError outside a QueryProvider.
useMutationState
Lists the runs of a mutation in progress on this client, oldest first, whichever component or Effect started them: a list can show the items a form elsewhere is still saving. A run is listed from when it starts until it settles, however it ends. A run concurrent: "queue" holds back is listed while it waits; one that concurrent: false makes wait for the run it replaces to roll back is listed once that's done. The list keeps its identity until the runs change.
A server render lists none, and so does the first render while the page hydrates. Throws a MissingQueryProviderError outside a QueryProvider.
UseMutationStateRun
A run of a mutation in progress, as useMutationState lists it.
Prop
Type
Load the cache in the browser
Guide: 3. Load the cache in the browser.
HydrationBoundary
Loads a cache QueryClient.dehydrate sent from the server into the client of the nearest QueryProvider, as TanStack's HydrationBoundary does, so the tree below shows the server's data without fetching it again. Throws a MissingQueryProviderError outside a provider.
Values for queries that aren't cached load during render, so its children read them in the same pass, the first render after server rendering included. Values for queries already cached load after commit, so a transition that brings new state doesn't change the page it leaves until React shows the new one. Otherwise it loads as QueryClient.hydrate does: a cached query takes only newer data, and a query the server was still loading waits for its result.
Under a QueryProvider that owns its client, once resetKey replaces the client, it loads into the new one only what the server dehydrated after the change, comparing the server's dehydration time with the browser's clock: earlier state belongs to the previous account, so the new client fetches it itself.
HydrationBoundaryProps
Props for HydrationBoundary.
Prop
Type