Refetch on demand
Refetch a query from an event, such as a button or pull-to-refresh.
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.
Refetch from a button or pull to refresh
Pass useRefetch the queries to refetch:
import { } from "@monarcode/effetch-query/react";
import { } from "./todos.js";
export function () {
const { , } = ();
return (
< ={} ={() => void ()}>
{ ? "Refreshing…" : "Refresh"}
</>
);
}useRefetch takes a target, a key, query, family or group that matches
queries: see 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. A
query that's only cached isn't refetched, and when none is in use,
refetch resolves at once.
It returns a UseRefetchResult:
refetchresolves 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.isRefetchingis 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:
import * as from "effect/Schema";
import { } from "@monarcode/effetch";
import { } from "@monarcode/effetch-query";
import { } from "@monarcode/effetch-query/react";
import { } from "./api.js";
import { , } from "./todos.js";
const = .(
.("GET /me", { : .({ : . }) }),
);
// Everything the home screen shows. Its spinner stops after all of it settles.
export const = .(, );
export const = .(
.("POST /todos/archive", { : .() }),
{ : [] },
);
export function () {
const { , } = ();
return (
< ={} ={() => void ()}>
Refresh
</>
);
}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.
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: the keys, queries, families and groups a refetch can take.
- Invalidate after a mutation: refetch what a change makes out of date, without a button.
- React Native: pull to refresh, and refetching when the app comes back to the foreground.