useIsFetching
useIsFetching returns how many requests are being sent right now by the fetch dispatcher of a
client: the ones from useFetch, useCache and client.fetchDispatcher. useIsSubmitting counts
the submit dispatcher (useSubmit). Use them for global loading and saving indicators.
Purpose
- Global indicators: one spinner or progress bar for the whole app.
- Scoped counts: count only the requests of one query or a custom filter.
Usage
components/global-spinner.tsx
import { useIsFetching, useIsSubmitting } from "@hyper-fetch/react";
import { client } from "./api/client";
function GlobalSpinner() {
const fetching = useIsFetching(client);
const submitting = useIsSubmitting(client);
if (!fetching && !submitting) return null;
return <Spinner label={submitting ? "Saving..." : "Loading..."} />;
}
Options
UseIsFetchingOptionsType
| Name | Type | Description |
|---|---|---|
| queryKey | | Count only the requests with matching queryKey - exact string or RegExp |
| predicate | | Count only the requests for which the predicate returns true |
Returns
A number: the requests being sent at the moment. Queued requests waiting for their turn and requests waiting for the
network are not counted. The hook re-renders only when the number changes, and returns 0 during server rendering.
Count part of the app
Filter by queryKey (exact string or RegExp) or with a predicate.
Scoped counts
const usersFetching = useIsFetching(client, { queryKey: getUsers.queryKey });
const adminFetching = useIsFetching(client, { queryKey: /admin/ });
const uploading = useIsSubmitting(client, {
predicate: (request) => request.endpoint.startsWith("/files"),
});
Summary
useIsFetchingcounts running fetch requests,useIsSubmittingrunning submits.- Filter with
queryKeyorpredicate.
Next steps
useIsFetching API reference
Generated types for the hook and its options.
- Runnable example: parallel requests shows a global indicator.
- Related:
useQueuelists the requests themselves,useIsSubmittingAPI.