Skip to main content
Version: v10.7.1

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
  1. Global indicators: one spinner or progress bar for the whole app.
  2. 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
string | RegExp
Count only the requests with matching queryKey - exact string or RegExp
predicate
(request: RequestInstance) => boolean
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
  1. useIsFetching counts running fetch requests, useIsSubmitting running submits.
  2. Filter with queryKey or predicate.

Next steps​

useIsFetching API reference
Generated types for the hook and its options.