Skip to Content
Effetch Query
Reference

@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 one has the first paragraph of its docs, and what it needs and how it fails; your editor shows the rest, with an example, when you hover the name.

Provide the client

Guide: Provide the client.

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.

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.

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), and while mounted it refetches on focus, reconnect and refetchInterval as the query's options say.

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.

Pause screens you can't see

Guide: Pause screens you can't see.

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, 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.

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.

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.

UseQueryStateResult

What useQueryState returns: one of three shapes, told apart by status (or isPending, isSuccess and isFailure), which narrows data, error and cause.

UseQueryStateSuccess

What useQueryState returns while the query has a value, including while it refetches.

Read many queries

Guide: Read many 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.

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.

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.

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.

UseInfiniteQueryResult

What useInfiniteQuery returns.

UseSuspenseInfiniteQueryResult

What useSuspenseInfiniteQuery returns.

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.

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.

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.

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.

Load the cache in the browser

Guide: 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.

HydrationBoundaryProps

Props for HydrationBoundary.

Prop

Type

On this page