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

A refresh button or pull to refresh needs a spinner for the refetch the
user asked for. A query's `waiting` can't give you that: it's true for
background refetches too, on focus, reconnect and polling. `useRefetch`
refetches on demand, with a flag that's true only for its own refetch.

The examples import `api.ts` and `todos.ts` from
[Getting started](https://effetch-query.monarcode.dev/docs/getting-started).

## Refetch from a button or pull to refresh

Pass `useRefetch` the queries to refetch:

```tsx
import { useRefetch } from "@monarcode/effetch-query/react";

import { todos } from "./todos.js";

export function RefreshButton() {
  const { refetch, isRefetching } = useRefetch(todos);
  return (
    <button disabled={isRefetching} onClick={() => void refetch()}>
      {isRefetching ? "Refreshing…" : "Refresh"}
    </button>
  );
}
```

`useRefetch` takes a target, a key, query, family or group that matches
queries: see [Targets](https://effetch-query.monarcode.dev/docs/queries/keys#targets). It refetches the
target's queries that are in use, as `Query.refetch` does. A query is in
use while something subscribes to it, such as a mounted component that
reads it: see [Queries in use](https://effetch-query.monarcode.dev/docs/queries/freshness#queries-in-use). A
query that's only cached isn't refetched, and when none is in use,
`refetch` resolves at once.

It returns a `UseRefetchResult`:

- `refetch` resolves when the queries have settled, and never rejects. It
  keeps its identity across renders, and uses the target of the latest
  render, so a key array written inline is fine.
- `isRefetching` is true only while one of this component's refetches is
  in progress. Focus, reconnect and polling don't set it.

That makes it the hook for pull to refresh. In React Native, pass
`isRefetching` and `refetch` to `FlatList`'s `refreshing` and `onRefresh`.

## Refresh several queries together

A screen that shows queries from several features, each with its own key,
refreshes them together with `Query.group`. A group is a target like any
other, so define it once and use it wherever a target goes. Here the home
screen refreshes the todos and the profile together, and a mutation
invalidates the same group:

```tsx
import * as Schema from "effect/Schema";

import { Fetch } from "@monarcode/effetch";
import { Query } from "@monarcode/effetch-query";
import { useRefetch } from "@monarcode/effetch-query/react";

import { Todo } from "./api.js";
import { api, todos } from "./todos.js";

const profile = api.query(
  Fetch.endpoint("GET /me", { output: Schema.Struct({ name: Schema.String }) }),
);

// Everything the home screen shows. Its spinner stops after all of it settles.
export const home = Query.group(todos, profile);

export const archiveDone = api.mutation(
  Fetch.endpoint("POST /todos/archive", { output: Schema.Array(Todo) }),
  { invalidates: [home] },
);

export function HomeRefresh() {
  const { refetch, isRefetching } = useRefetch(home);
  return (
    <button disabled={isRefetching} onClick={() => void refetch()}>
      Refresh
    </button>
  );
}
```

`isRefetching` stays true until every query in the group has settled.

A mutation's `invalidates` takes a list of targets, so the group goes in
as one item: see [Invalidate after a mutation](https://effetch-query.monarcode.dev/docs/mutations/invalidate).
`useRefetch` takes one target, so don't pass it an array of queries.
`useRefetch([todos, profile])` compiles, because an array is a key, but a
key that holds queries would match nothing. `useRefetch` throws a
`Query.KeyHoldsTargetError` as it renders instead.

## What's next

- [Targets](https://effetch-query.monarcode.dev/docs/queries/keys#targets): the keys, queries, families and
  groups a refetch can take.
- [Invalidate after a mutation](https://effetch-query.monarcode.dev/docs/mutations/invalidate): refetch what a
  change makes out of date, without a button.
- [React Native](https://effetch-query.monarcode.dev/docs/react/react-native): pull to refresh, and refetching
  when the app comes back to the foreground.