# @monarcode/effetch-query/react (https://effetch-query.monarcode.dev/docs/reference/react)

`@monarcode/effetch-query/react` exports the provider, the hooks and the boundaries. Import what you use:

```ts
import {
  HydrationBoundary,
  MissingQueryProviderError,
  QueryBoundary,
  QueryFailure,
  QueryProvider,
  useInfiniteQuery,
  useInfiniteQueryState,
  useIsFetching,
  useIsMutating,
  useMutation,
  useMutationState,
  useQueries,
  useQueriesState,
  useQuery,
  useQueryClient,
  useQueryState,
  useRefetch,
  useResetQueryFailure,
  useSuspenseInfiniteQuery,
  useSuspenseInfiniteQueryState,
  useSuspenseQuery,
  useSuspenseQueryState,
} 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](https://effetch-query.monarcode.dev/docs/getting-started#4-show-a-query).

### QueryProvider

Makes a [`QueryClient.QueryClient`](https://effetch-query.monarcode.dev/docs/reference/query-client#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`](https://effetch-query.monarcode.dev/docs/reference/react#usequeryclient). The hooks of `@monarcode/effetch-query/react`, and `HydrationBoundary`, throw a [`MissingQueryProviderError`](https://effetch-query.monarcode.dev/docs/reference/react#missingqueryprovidererror) outside one.

Give it a `client` you own ([`QueryProviderProps`](https://effetch-query.monarcode.dev/docs/reference/react#queryproviderprops)), or a `make` it calls to own one, made again for each `resetKey` ([`OwnedQueryProviderProps`](https://effetch-query.monarcode.dev/docs/reference/react#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`](https://effetch-query.monarcode.dev/docs/reference/react#queryprovider) with a client you own: the provider uses it and never disposes it. For a client the provider makes and disposes, see [`OwnedQueryProviderProps`](https://effetch-query.monarcode.dev/docs/reference/react#ownedqueryproviderprops).

| Prop        | Type        | Description                                                      |
| ----------- | ----------- | ---------------------------------------------------------------- |
| `client`    | `object`    | The client the hooks below use. The provider doesn't dispose it. |
| `make?`     | `undefined` | Only with an owned client: OwnedQueryProviderProps.make.         |
| `resetKey?` | `undefined` | Only with an owned client: OwnedQueryProviderProps.resetKey.     |
| `children?` | `ReactNode` | The tree that uses the client.                                   |

## Sign out and switch accounts

Guide: [Sign out and switch accounts](https://effetch-query.monarcode.dev/docs/cache/accounts).

### OwnedQueryProviderProps

Props for [`QueryProvider`](https://effetch-query.monarcode.dev/docs/reference/react#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        | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| ----------- | ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `make`      | `function`  | Makes the client, on the first render and again when `resetKey` changes. A new function on each render doesn't make a new client, so an inline arrow is fine.                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| `resetKey?` | `string`    | The key the client belongs to, such as the signed-in account's ID. Changing it replaces the client with a new one from `make`, and disposes the old one: its cache goes, and its fetches and mutation runs are interrupted. The tree below never renders with the old client again. A `HydrationBoundary` below loads into the new client only what the server dehydrated after the change (the server's dehydration time against the browser's clock), so state the previous account's page sent doesn't reach the new account. Without it, the provider keeps one client until it unmounts. |
| `client?`   | `undefined` | Only with a client you own: QueryProviderProps.client.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| `children?` | `ReactNode` | The tree that uses the client.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |

## Read a query

Guide: [Read a query](https://effetch-query.monarcode.dev/docs/react).

### MissingQueryProviderError

Thrown by [`useQueryClient`](https://effetch-query.monarcode.dev/docs/reference/react#usequeryclient), the other hooks of `@monarcode/effetch-query/react` and `HydrationBoundary` outside a [`QueryProvider`](https://effetch-query.monarcode.dev/docs/reference/react#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`](https://effetch-query.monarcode.dev/docs/reference/query-client#queryclient) of the nearest [`QueryProvider`](https://effetch-query.monarcode.dev/docs/reference/react#queryprovider), for running Effects from event handlers. Throws a [`MissingQueryProviderError`](https://effetch-query.monarcode.dev/docs/reference/react#missingqueryprovidererror) outside one.

### UseQueryOptions

Options for [`useInfiniteQuery`](https://effetch-query.monarcode.dev/docs/reference/react#useinfinitequery), and the base of [`UseQuerySelectOptions`](https://effetch-query.monarcode.dev/docs/reference/react#usequeryselectoptions), which [`useQuery`](https://effetch-query.monarcode.dev/docs/reference/react#usequery) and [`useQueryState`](https://effetch-query.monarcode.dev/docs/reference/react#usequerystate) take.

| Prop            | Type    | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| --------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `keepPrevious?` | `union` | If true, while the query has no value yet (say, the next page of a family), the hook shows the last success it showed, as a waiting `Success`, instead of `Initial`: TanStack's `placeholderData: keepPreviousData`. A failure is shown as it is, and a `null` query still reads `Initial`. Default: `false`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| `paused?`       | `union` | If true, the hook keeps showing what it last showed and stops counting as a use of the query: an invalidation, focus, reconnect or `refetchInterval` doesn't refetch it for this component, nor does the component re-render. What it shows isn't `waiting`, since nothing fetches for it, and a `clear` replaces it with what the cache has. Use it for a screen that stays mounted under another, as React Native's stack navigators keep them. Paused from its first render, it shows what the cache has, without fetching. `useInfiniteQuery`'s page functions still fetch pages when called. A query whose readers are all paused isn't in use, so it's dropped after `gcTime` like any other, though the paused readers still show what they had. When `paused` turns false, the mount policy runs as on a mount: the query refetches if it's stale or failed, unless another reader fetched it meanwhile and it's still fresh. Default: `false`. |

## Select part of a value

Guide: [Select part of a value](https://effetch-query.monarcode.dev/docs/react#select-part-of-a-value).

### UseQuerySelectOptions

Options for [`useQuery`](https://effetch-query.monarcode.dev/docs/reference/react#usequery) and [`useQueryState`](https://effetch-query.monarcode.dev/docs/reference/react#usequerystate): those of [`UseQueryOptions`](https://effetch-query.monarcode.dev/docs/reference/react#usequeryoptions), and `select`.

| Prop            | Type       | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| --------------- | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `select?`       | `function` | Derives what the component shows from the query's value, such as one field or a count: the hook returns its result instead of the value, and a failure's previous success is selected too. The cache keeps the whole value, so other readers of the query see it unchanged. The component re-renders when the selection changes or a fetch starts or ends (`waiting`), not when the value changes in a way the selection doesn't show: a selection equal to the last one (`Equal.equals`, which compares plain objects and arrays by their contents) keeps the last result, timestamp included. An inline function runs on every render; for a costly selection, pass one defined outside the component or with `useCallback`, which runs only when the query's result changes. If it throws, the hook shows a failure whose cause is the defect, with no earlier value; if it throws on a failure's previous success, the failure shows without one. With `keepPrevious`, the last selection shown is kept. Default: none, the value itself. |
| `keepPrevious?` | `union`    | If true, while the query has no value yet (say, the next page of a family), the hook shows the last success it showed, as a waiting `Success`, instead of `Initial`: TanStack's `placeholderData: keepPreviousData`. A failure is shown as it is, and a `null` query still reads `Initial`. Default: `false`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| `paused?`       | `union`    | If true, the hook keeps showing what it last showed and stops counting as a use of the query: an invalidation, focus, reconnect or `refetchInterval` doesn't refetch it for this component, nor does the component re-render. What it shows isn't `waiting`, since nothing fetches for it, and a `clear` replaces it with what the cache has. Use it for a screen that stays mounted under another, as React Native's stack navigators keep them. Paused from its first render, it shows what the cache has, without fetching. `useInfiniteQuery`'s page functions still fetch pages when called. A query whose readers are all paused isn't in use, so it's dropped after `gcTime` like any other, though the paused readers still show what they had. When `paused` turns false, the mount policy runs as on a mount: the query refetches if it's stale or failed, unless another reader fetched it meanwhile and it's still fresh. Default: `false`.                                                                                       |

## Read a query as an object

Guide: [Read a query as an object](https://effetch-query.monarcode.dev/docs/react/as-object).

### useQueryState

Reads a query as [`useQuery`](https://effetch-query.monarcode.dev/docs/reference/react#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`](https://effetch-query.monarcode.dev/docs/reference/react#usequery) does, `select` and `keepPrevious` included.

Check `status` before reading `data`: it narrows the object ([`UseQueryStateResult`](https://effetch-query.monarcode.dev/docs/reference/react#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`](https://effetch-query.monarcode.dev/docs/reference/react#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`](https://effetch-query.monarcode.dev/docs/reference/infinite-query#infinitequery) as [`useInfiniteQuery`](https://effetch-query.monarcode.dev/docs/reference/react#useinfinitequery) does, and returns its result as an object of fields, as [`useQueryState`](https://effetch-query.monarcode.dev/docs/reference/react#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`](https://effetch-query.monarcode.dev/docs/reference/react#useinfinitequery) does.

### useQueriesState

Reads a list of queries as [`useQueries`](https://effetch-query.monarcode.dev/docs/reference/react#usequeries) does, and returns each query's result as an object of fields, as [`useQueryState`](https://effetch-query.monarcode.dev/docs/reference/react#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`](https://effetch-query.monarcode.dev/docs/reference/react#usequeries) does.

### useSuspenseInfiniteQueryState

Reads an [`InfiniteQuery.InfiniteQuery`](https://effetch-query.monarcode.dev/docs/reference/infinite-query#infinitequery) through React Suspense, as [`useSuspenseInfiniteQuery`](https://effetch-query.monarcode.dev/docs/reference/react#usesuspenseinfinitequery) does, and returns its result as an object of fields, as [`useSuspenseQueryState`](https://effetch-query.monarcode.dev/docs/reference/react#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`](https://effetch-query.monarcode.dev/docs/reference/react#usesuspenseinfinitequery) does.

### useSuspenseQueryState

Reads a query through React Suspense, as [`useSuspenseQuery`](https://effetch-query.monarcode.dev/docs/reference/react#usesuspensequery) does, and returns its result as an object of fields, as [`useQueryState`](https://effetch-query.monarcode.dev/docs/reference/react#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`](https://effetch-query.monarcode.dev/docs/reference/react#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`](https://effetch-query.monarcode.dev/docs/reference/react#usesuspensequery) does. The object keeps its identity while nothing in it changes. Throws a `MissingQueryProviderError` outside a `QueryProvider`.

### UseInfiniteQueryStateResult

What [`useInfiniteQueryState`](https://effetch-query.monarcode.dev/docs/reference/react#useinfinitequerystate) returns: [`useQueryState`](https://effetch-query.monarcode.dev/docs/reference/react#usequerystate)'s object for the pages, narrowed by `status`, with the page controls beside it.

### UseQueriesStateEntry

What [`useQueriesState`](https://effetch-query.monarcode.dev/docs/reference/react#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`](https://effetch-query.monarcode.dev/docs/reference/react#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`](https://effetch-query.monarcode.dev/docs/reference/react#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        | Description                                                                                                                      |
| --------------- | ----------- | -------------------------------------------------------------------------------------------------------------------------------- |
| `status`        | `"failure"` | `"failure"`: the query's last fetch failed, or `select` threw.                                                                   |
| `data`          | `union`     | The last success before the failure, or `undefined` if there was none: a refetch that fails keeps the value it had.              |
| `error`         | `union`     | The typed error the query failed with, or `undefined` when it failed with a defect or an interruption, which only `cause` holds. |
| `cause`         | `object`    | The cause of the failure: the query's error, a defect, or an interruption.                                                       |
| `isPending`     | `false`     | Always false.                                                                                                                    |
| `isSuccess`     | `false`     | Always false.                                                                                                                    |
| `isFailure`     | `true`      | Always true.                                                                                                                     |
| `isFetching`    | `union`     | True while a refetch is in progress: the result's `waiting`.                                                                     |
| `isPlaceholder` | `false`     | Always false: a failure's `data` is the query's own last success.                                                                |
| `refetch`       | `function`  | Refetches the query, as UseQueryStateSuccess.refetch does.                                                                       |
| `result`        | `object`    | The result the fields come from, a `Failure`.                                                                                    |

### UseQueryStatePending

What [`useQueryState`](https://effetch-query.monarcode.dev/docs/reference/react#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        | Description                                                                                                                                                                                              |
| --------------- | ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `status`        | `"pending"` | `"pending"`: no value and no failure yet.                                                                                                                                                                |
| `data`          | `undefined` | Always `undefined`: there's no value yet.                                                                                                                                                                |
| `error`         | `undefined` | Always `undefined`: there's no failure.                                                                                                                                                                  |
| `cause`         | `undefined` | Always `undefined`: there's no failure.                                                                                                                                                                  |
| `isPending`     | `true`      | Always true.                                                                                                                                                                                             |
| `isSuccess`     | `false`     | Always false.                                                                                                                                                                                            |
| `isFailure`     | `false`     | Always false.                                                                                                                                                                                            |
| `isFetching`    | `union`     | True while a fetch of the query is in progress: the result's `waiting`. False for a `null` query. In a server render, a query the cache doesn't have reads as fetching, though nothing fetches it there. |
| `isPlaceholder` | `false`     | Always false: see UseQueryStateSuccess.isPlaceholder.                                                                                                                                                    |
| `refetch`       | `function`  | Refetches the query, as UseQueryStateSuccess.refetch does.                                                                                                                                               |
| `result`        | `object`    | The result the fields come from, an `Initial`.                                                                                                                                                           |

### UseQueryStateSuccess

What [`useQueryState`](https://effetch-query.monarcode.dev/docs/reference/react#usequerystate) returns while the query has a value, including while it refetches.

| Prop            | Type        | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| --------------- | ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `status`        | `"success"` | `"success"`: the query has a value.                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| `data`          | `A`         | The query's value, or its selection with `select`.                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| `error`         | `undefined` | Always `undefined`: there's no failure.                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| `cause`         | `undefined` | Always `undefined`: there's no failure.                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| `isPending`     | `false`     | Always false.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| `isSuccess`     | `true`      | Always true.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| `isFailure`     | `false`     | Always false.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| `isFetching`    | `union`     | True while the query refetches in the background: the result's `waiting`.                                                                                                                                                                                                                                                                                                                                                                                                                     |
| `isPlaceholder` | `union`     | True when `data` isn't the query's own value yet: its placeholder (Query.MakeOptions.placeholder), or, with `keepPrevious`, the value of the query shown before. It stays true until the query's own value or failure replaces it, and `isFetching` is true meanwhile, except for a `paused` reader, which fetches nothing: it keeps the placeholder, not fetching, until it resumes. Use it to dim the data, or to turn off what needs the real value, such as editing it or paging past it. |
| `refetch`       | `function`  | Refetches the query, as Query.refetch does, and resolves when the refetch settles. Never rejects: a failure shows in the fields. It keeps its identity while the query stays the same. Resolves at once for a `null` query, and, since a `paused` reader isn't a use, for a query only paused readers use.                                                                                                                                                                                    |
| `result`        | `object`    | The result the fields come from, a `Success`.                                                                                                                                                                                                                                                                                                                                                                                                                                                 |

### UseSuspenseInfiniteQueryStateResult

What [`useSuspenseInfiniteQueryState`](https://effetch-query.monarcode.dev/docs/reference/react#usesuspenseinfinitequerystate) returns: [`useSuspenseQueryState`](https://effetch-query.monarcode.dev/docs/reference/react#usesuspensequerystate)'s object for the pages, with the page controls beside it.

### UseSuspenseQueryStateFailure

What [`useSuspenseQueryState`](https://effetch-query.monarcode.dev/docs/reference/react#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        | Description                                                                                                                      |
| --------------- | ----------- | -------------------------------------------------------------------------------------------------------------------------------- |
| `data`          | `A`         | The last success before the failure: there always is one.                                                                        |
| `status`        | `"failure"` | `"failure"`: the query's last fetch failed, or `select` threw.                                                                   |
| `error`         | `union`     | The typed error the query failed with, or `undefined` when it failed with a defect or an interruption, which only `cause` holds. |
| `cause`         | `object`    | The cause of the failure: the query's error, a defect, or an interruption.                                                       |
| `isPending`     | `false`     | Always false.                                                                                                                    |
| `isSuccess`     | `false`     | Always false.                                                                                                                    |
| `isFailure`     | `true`      | Always true.                                                                                                                     |
| `isFetching`    | `union`     | True while a refetch is in progress: the result's `waiting`.                                                                     |
| `isPlaceholder` | `false`     | Always false: a failure's `data` is the query's own last success.                                                                |
| `refetch`       | `function`  | Refetches the query, as UseQueryStateSuccess.refetch does.                                                                       |
| `result`        | `object`    | The result the fields come from, a `Failure`.                                                                                    |

### UseSuspenseQueryStateResult

What [`useSuspenseQueryState`](https://effetch-query.monarcode.dev/docs/reference/react#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](https://effetch-query.monarcode.dev/docs/react#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`](https://effetch-query.monarcode.dev/docs/reference/react#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`](https://effetch-query.monarcode.dev/docs/reference/react#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`](https://effetch-query.monarcode.dev/docs/reference/react#usequeries).

| Prop      | Type    | Description                                                                                                                                               |
| --------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `paused?` | `union` | If true, the hook keeps showing what it last showed and stops counting as a use of the queries, as UseQueryOptions.paused does for one. Default: `false`. |

### UseQueriesResult

What [`useQueries`](https://effetch-query.monarcode.dev/docs/reference/react#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](https://effetch-query.monarcode.dev/docs/react/suspense).

### 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 `QueryFailure`s. 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`](https://effetch-query.monarcode.dev/docs/reference/react#usesuspensequery) and [`useSuspenseInfiniteQuery`](https://effetch-query.monarcode.dev/docs/reference/react#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`](https://effetch-query.monarcode.dev/docs/reference/react#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`](https://effetch-query.monarcode.dev/docs/reference/react#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       | Description                                                                                                                                                                 |
| ------- | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `error` | `unknown`  | The failure squashed to one value (`Cause.squash`), for display: the query's error, or the defect it died with.                                                             |
| `cause` | `object`   | The cause of the failure: the query's error, a defect, or an interruption.                                                                                                  |
| `retry` | `function` | Refetches the failed query and renders the children again, which show `pending` while the refetch is in progress. Only that query: another failed query below throws again. |

### QueryBoundaryProps

Props for [`QueryBoundary`](https://effetch-query.monarcode.dev/docs/reference/react#queryboundary).

| Prop        | Type        | Description                                                                                                                                                                                                                        |
| ----------- | ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `pending?`  | `ReactNode` | The content to show while a query below suspends: the `Suspense` fallback. Default: nothing.                                                                                                                                       |
| `failure`   | `function`  | Renders what to show when a query below fails with nothing to show, such as an error message and a "Try again" button that calls `retry`. Called only for a `QueryFailure`; any other error goes on to the next error boundary up. |
| `children?` | `ReactNode` | The tree that reads queries, with `useSuspenseQuery` for example.                                                                                                                                                                  |

### UseSuspenseQueryOptions

Options for [`useSuspenseQuery`](https://effetch-query.monarcode.dev/docs/reference/react#usesuspensequery).

| Prop      | Type       | Description                                                                                                                                                                                                                                              |
| --------- | ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `select?` | `function` | Derives what the component shows from the query's value, as UseQuerySelectOptions.select does. If it throws, the defect is thrown to the error boundary as a `QueryFailure`, even after an earlier selection was shown. Default: none, the value itself. |

## Refetch on demand

Guide: [Refetch on demand](https://effetch-query.monarcode.dev/docs/react/refetch).

### 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`](https://effetch-query.monarcode.dev/docs/reference/query#target)'s queries in use, as [`Query.refetch`](https://effetch-query.monarcode.dev/docs/reference/query#refetch) does. For a screen made of queries with different keys, pass a [`Query.group`](https://effetch-query.monarcode.dev/docs/reference/query#group) of them: it's refetching until every one has settled. Throws a `MissingQueryProviderError` outside a `QueryProvider`, and a [`Query.KeyHoldsTargetError`](https://effetch-query.monarcode.dev/docs/reference/query#keyholdstargeterror) for a key that holds a query, a family or a group, such as `[todos, profile]`: pass a [`Query.group`](https://effetch-query.monarcode.dev/docs/reference/query#group).

### UseRefetchResult

What [`useRefetch`](https://effetch-query.monarcode.dev/docs/reference/react#userefetch) returns.

| Prop           | Type       | Description                                                                                                                                                                                                         |
| -------------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `refetch`      | `function` | Refetches the target's queries in use, and resolves when they've settled. Never rejects. It keeps its identity across renders while the provider's client stays the same, and uses the target of the latest render. |
| `isRefetching` | `union`    | Whether one of this component's refetches is in progress. A background refetch (focus, reconnect, polling) doesn't count.                                                                                           |

## Load more

Guide: [Load more](https://effetch-query.monarcode.dev/docs/queries/infinite#load-more).

### useInfiniteQuery

Reads an [`InfiniteQuery.InfiniteQuery`](https://effetch-query.monarcode.dev/docs/reference/infinite-query#infinitequery) as [`useQuery`](https://effetch-query.monarcode.dev/docs/reference/react#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`](https://effetch-query.monarcode.dev/docs/reference/react#usequery) does.

### useSuspenseInfiniteQuery

Reads an [`InfiniteQuery.InfiniteQuery`](https://effetch-query.monarcode.dev/docs/reference/infinite-query#infinitequery) through React Suspense, as [`useSuspenseQuery`](https://effetch-query.monarcode.dev/docs/reference/react#usesuspensequery) does, with the same page controls as [`useInfiniteQuery`](https://effetch-query.monarcode.dev/docs/reference/react#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`](https://effetch-query.monarcode.dev/docs/reference/react#usesuspensequery) does.

### InfinitePageControls

The page controls [`useInfiniteQuery`](https://effetch-query.monarcode.dev/docs/reference/react#useinfinitequery) and [`useSuspenseInfiniteQuery`](https://effetch-query.monarcode.dev/docs/reference/react#usesuspenseinfinitequery) return beside the result.

| Prop                     | Type       | Description                                                                                                                                                                   |
| ------------------------ | ---------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `fetchNextPage`          | `function` | Fetches the page after the last one, as InfiniteQuery.fetchNextPage does. Resolves when it settles; never rejects: a failure shows in `result`, which keeps the loaded pages. |
| `fetchPreviousPage`      | `function` | Fetches the page before the first one, as InfiniteQuery.fetchPreviousPage does. Like `fetchNextPage`, it resolves when it settles and never rejects.                          |
| `hasNextPage`            | `union`    | Whether `nextPageParam` gives a param for the shown pages (neither `undefined` nor `null`).                                                                                   |
| `hasPreviousPage`        | `union`    | Whether `previousPageParam` gives a param for the shown pages.                                                                                                                |
| `isFetchingNextPage`     | `union`    | Whether a next page is being fetched, as opposed to a refetch of every page.                                                                                                  |
| `isFetchingPreviousPage` | `union`    | Whether a previous page is being fetched.                                                                                                                                     |

### UseInfiniteQueryResult

What [`useInfiniteQuery`](https://effetch-query.monarcode.dev/docs/reference/react#useinfinitequery) returns.

| Prop                     | Type                     | Description                                                                                                                                                                   |
| ------------------------ | ------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `result`                 | `AsyncResult<object, E>` | The query's result, as useQuery returns it.                                                                                                                                   |
| `fetchNextPage`          | `function`               | Fetches the page after the last one, as InfiniteQuery.fetchNextPage does. Resolves when it settles; never rejects: a failure shows in `result`, which keeps the loaded pages. |
| `fetchPreviousPage`      | `function`               | Fetches the page before the first one, as InfiniteQuery.fetchPreviousPage does. Like `fetchNextPage`, it resolves when it settles and never rejects.                          |
| `hasNextPage`            | `union`                  | Whether `nextPageParam` gives a param for the shown pages (neither `undefined` nor `null`).                                                                                   |
| `hasPreviousPage`        | `union`                  | Whether `previousPageParam` gives a param for the shown pages.                                                                                                                |
| `isFetchingNextPage`     | `union`                  | Whether a next page is being fetched, as opposed to a refetch of every page.                                                                                                  |
| `isFetchingPreviousPage` | `union`                  | Whether a previous page is being fetched.                                                                                                                                     |

### UseSuspenseInfiniteQueryResult

What [`useSuspenseInfiniteQuery`](https://effetch-query.monarcode.dev/docs/reference/react#usesuspenseinfinitequery) returns.

| Prop                     | Type       | Description                                                                                                                                                                   |
| ------------------------ | ---------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `result`                 | `object`   | The query's success, as useSuspenseQuery returns it.                                                                                                                          |
| `fetchNextPage`          | `function` | Fetches the page after the last one, as InfiniteQuery.fetchNextPage does. Resolves when it settles; never rejects: a failure shows in `result`, which keeps the loaded pages. |
| `fetchPreviousPage`      | `function` | Fetches the page before the first one, as InfiniteQuery.fetchPreviousPage does. Like `fetchNextPage`, it resolves when it settles and never rejects.                          |
| `hasNextPage`            | `union`    | Whether `nextPageParam` gives a param for the shown pages (neither `undefined` nor `null`).                                                                                   |
| `hasPreviousPage`        | `union`    | Whether `previousPageParam` gives a param for the shown pages.                                                                                                                |
| `isFetchingNextPage`     | `union`    | Whether a next page is being fetched, as opposed to a refetch of every page.                                                                                                  |
| `isFetchingPreviousPage` | `union`    | Whether a previous page is being fetched.                                                                                                                                     |

## Run a mutation

Guide: [Run a mutation](https://effetch-query.monarcode.dev/docs/mutations/run).

### 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`](https://effetch-query.monarcode.dev/docs/reference/react#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`](https://effetch-query.monarcode.dev/docs/reference/react#usemutationstate).

Define mutations at module level, with [`Mutation.make`](https://effetch-query.monarcode.dev/docs/reference/mutation#make): a new definition on each render starts a new state. Outside React, run one with [`Mutation.run`](https://effetch-query.monarcode.dev/docs/reference/mutation#run). Throws a `MissingQueryProviderError` outside a `QueryProvider`.

### UseMutationResult

What [`useMutation`](https://effetch-query.monarcode.dev/docs/reference/react#usemutation) returns.

| Prop        | Type                | Description                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| ----------- | ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `result`    | `AsyncResult<A, E>` | The state of this component's latest run: `Initial` before the first and after UseMutationResult.reset.                                                                                                                                                                                                                                                                                                                                               |
| `status`    | `union`             | Where the latest run is, as TanStack Query's mutation `status`: `"idle"` before the first run and after `reset`, `"pending"` while it runs, then `"success"` or `"failure"`. Unlike useQueryState's, it doesn't narrow `data` and `error`: check them, or match on `result`.                                                                                                                                                                          |
| `data`      | `union`             | The latest run's value once it succeeds; `undefined` otherwise, including while a newer run is pending.                                                                                                                                                                                                                                                                                                                                               |
| `error`     | `union`             | The latest run's typed error once it fails; `undefined` otherwise, and for a run that died or was interrupted, which only `cause` holds.                                                                                                                                                                                                                                                                                                              |
| `cause`     | `union`             | The latest run's failure, defects and interruptions included, once it fails.                                                                                                                                                                                                                                                                                                                                                                          |
| `isIdle`    | `union`             | True before the first run and after `reset`.                                                                                                                                                                                                                                                                                                                                                                                                          |
| `isPending` | `union`             | True while the latest run is in progress: `result.waiting`.                                                                                                                                                                                                                                                                                                                                                                                           |
| `isSuccess` | `union`             | True once the latest run has succeeded.                                                                                                                                                                                                                                                                                                                                                                                                               |
| `isFailure` | `union`             | True once the latest run has failed.                                                                                                                                                                                                                                                                                                                                                                                                                  |
| `variables` | `union`             | The variables of the run UseMutationResult.result shows: `undefined` before the first run and after UseMutationResult.reset. Render the change a run is making from them, without touching the cache, while `result.waiting` is true.                                                                                                                                                                                                                 |
| `run`       | `function`          | Starts a run. Resolves with its `Exit`; never rejects. With the mutation's `concurrent: false`, a run that a newer one replaces resolves with an interrupted `Exit`; with `concurrent: "drop"`, so does a run started while the one `result` shows is in progress, at once, and it changes nothing. On a disposed client the run never starts, and resolves with a died `Exit`. `options` are callbacks for this run only: see UseMutationRunOptions. |
| `reset`     | `function`          | Puts `result` back to `Initial`. Runs in progress carry on.                                                                                                                                                                                                                                                                                                                                                                                           |

### UseMutationRunOptions

Callbacks for one run of [`useMutation`](https://effetch-query.monarcode.dev/docs/reference/react#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       | Description                                                    |
| ------------ | ---------- | -------------------------------------------------------------- |
| `onSuccess?` | `function` | Called with the value when the run succeeds.                   |
| `onFailure?` | `function` | Called with the cause when the run fails or dies.              |
| `onSettled?` | `function` | Called with the run's `Exit` after `onSuccess` or `onFailure`. |

## Show activity

Guide: [Show activity](https://effetch-query.monarcode.dev/docs/mutations/activity).

### useIsFetching

Counts the queries with a fetch in progress on this client: those the [`Query.Target`](https://effetch-query.monarcode.dev/docs/reference/query#target) matches, or all of them without one, as [`Query.invalidate`](https://effetch-query.monarcode.dev/docs/reference/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`](https://effetch-query.monarcode.dev/docs/reference/react#usemutationstate) lists it.

| Prop        | Type                | Description                                                                          |
| ----------- | ------------------- | ------------------------------------------------------------------------------------ |
| `variables` | `Variables`         | The variables the run was started with.                                              |
| `result`    | `AsyncResult<A, E>` | The run's state. It's listed only while in progress, so this is a waiting `Initial`. |

## Load the cache in the browser

Guide: [3. Load the cache in the browser](https://effetch-query.monarcode.dev/docs/server-rendering#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`](https://effetch-query.monarcode.dev/docs/reference/react#hydrationboundary).

| Prop        | Type        | Description                                                                  |
| ----------- | ----------- | ---------------------------------------------------------------------------- |
| `state?`    | `object`    | What `QueryClient.dehydrate` sent from the server. Nothing loads without it. |
| `children?` | `ReactNode` | The tree that reads it.                                                      |