Show activity
Show a global indicator while queries or mutations run.
A mutation's state belongs to the component that runs it, but a saving notice or a progress bar often sits somewhere else in the app. To show what's saving anywhere, read the client's activity instead. This page shows how to list one mutation's runs, and how to count everything in progress.
The examples import toggle-todo.ts from
Getting started.
List the runs of one mutation
useMutationState lists a mutation's runs in progress, oldest first,
with their variables, whichever component or Effect started them:
import { } from "@monarcode/effetch-query/react";
import { } from "./toggle-todo.js";
export function () {
const = ();
return . === 0 ? null : (
< ="status">
Saving {.(() => `#${...}`).(", ")}
</>
);
}Each entry is a UseMutationStateRun, with the variables the run was
started with and its result, a waiting Initial. A run is listed from
when it starts until it settles, however it ends. Two kinds of run wait
before they start, and are listed differently:
- A run
concurrent: "queue"holds back is listed while it waits. - A run that
concurrent: falsemakes wait for the run it replaces to roll back is listed after that's done.
Count fetches and runs
useIsFetching counts the queries fetching on the provider's client, and
useIsMutating the mutation runs in progress. Together they drive an
app-wide progress bar:
import { , } from "@monarcode/effetch-query/react";
export function () {
const = () + () > 0;
return < ="progressbar" ="Loading" ={!} />;
}Both count background refetches, and runs that any component or
Mutation.run started. Pass a target to count fewer:
useIsFetching(["todos"]) counts the todos queries, and
useIsMutating(toggleTodo) the runs of one mutation.
A server render counts nothing in progress, and so does the first render
as the page hydrates, so the markup matches; the counts show right after.
All three hooks throw a MissingQueryProviderError outside a
QueryProvider.
What's next
- Optimistic updates: show the change itself, not only that something is saving.
- When runs overlap: how
"queue"andfalsedecide when a run starts. - Mutation options: every option a mutation takes.