Run a mutation
Run a mutation from a component, and react to its result.
useMutation gives each component its own state for a mutation. This
checkbox toggles a todo, and clears a failure after three seconds:
The examples import api.ts, client.ts and toggle-todo.ts from Getting started.
import * as from "effect/Exit";
import { } from "@monarcode/effetch-query/react";
import type { } from "./api.js";
import { } from "./toggle-todo.js";
export function ({ }: { readonly : }) {
const { , , } = ();
return (
<
="checkbox"
={.}
={.}
={async () => {
const = await ({
: { : . },
: { : .. },
});
if (.()) {
(, 3000);
}
}}
/>
);
}useMutation returns a UseMutationResult:
result, theAsyncResultof this component's latest run:Initialbefore the first run and afterreset;variables, the variables of the runresultshows, orundefinedbefore the first run and afterreset;run(variables, options?), which starts a run and returns aPromiseof itsExit;reset(), which putsresultback toInitial. Runs in progress carry on.
run never rejects: check the Exit it resolves with. A run carries on if
the component unmounts, so the mutation's onSuccess and invalidates
still happen. The mutation's concurrent decides how the component's runs
overlap (Run modes): with false, a run that a newer one
replaces resolves with an interrupted Exit. On a disposed client, the run
never starts and resolves with a died Exit. Outside a QueryProvider,
useMutation throws a MissingQueryProviderError.
Callbacks for one run. run also takes callbacks for one run, a
UseMutationRunOptions, as TanStack's mutate(variables, { onSuccess })
does:
import { } from "@monarcode/effetch-query/react";
import type { } from "./api.js";
import { } from "./toggle-todo.js";
export function ({
,
,
}: {
readonly : ;
readonly : () => void;
}) {
const { , } = ();
return (
<
="button"
={.}
={() =>
(
{ : { : . }, : { : true } },
{ : },
)
}
>
Done
</>
);
}onSuccess, onFailure and onSettled run after the mutation's own
callbacks, its invalidates and the client's hooks, before code awaiting
run carries on. They belong to this screen, so they're dropped if,
before the run settles, reset is called, a newer run starts or the
component unmounts. They're dropped for a run that was interrupted or
never started, too. The run completes all the same. A callback that throws
is logged with console.error; the ones after it still run, and run
still resolves.
So use them for what matters only to this screen, such as filling in a
form field or navigating, and the mutation's own onSuccess for what must
always happen. A lookup that you reset when its input changes never
fills in the answer for the old input. A lookup that only reads can also
be a query keyed by its input, read with
useQuery(input === "" ? null : lookup(input)).
From Effect. Mutation.run(mutation, variables) runs a mutation from
Effect code, with the same steps, and succeeds or fails as its run did.
A mutation whose run takes no variables runs without them. Here a bulk
action marks several todos done, one after another:
import * as from "effect/Effect";
import { } from "@monarcode/effetch-query";
import { } from "./client.js";
import { } from "./toggle-todo.js";
const = (: <number>) =>
.(
,
() => .(, { : { }, : { : true } }),
{ : true },
);
// From an event handler, through the client.
export const = (: <number>) =>
.(());Each Mutation.run is a run of its own: concurrent applies to one use's
runs, so calls never interrupt each other. Runs started this way count in
useIsMutating and useMutationState (Show activity).