useMutation
modelence / client / useMutation
Function: useMutation()
function useMutation<T>(methodName, args): object
Defined in: src/client/method.ts:159
React hook for executing a mutation method.
This hook provides functions to trigger the mutation manually and handles loading/error states. Similar to React Query's useMutation hook.
Type Parameters
T
T
= unknown
The expected return type of the mutation
Parameters
methodName
string
The name of the method to mutate
args
Args
= {}
Optional default arguments to pass to the method
Returns
object
An object containing the mutation state and trigger functions:
data
- The data returned by the last successful mutation, or nullisFetching
- Boolean indicating if the mutation is in progresserror
- Any error that occurred during the last mutation, or nullmutate
- Function to trigger the mutation with optional argumentsmutateAsync
- Promise-returning version of mutate, useful for awaiting the result
data
data: null | NonNullable<T>;
error
error: null | Error;
isFetching
isFetching: boolean = mutation.isPending;
isPending
isPending: boolean = mutation.isPending;
mutate
mutate: UseMutateFunction<T, Error, undefined | Args, unknown> = mutation.mutate;
mutateAsync
mutateAsync: UseMutateAsyncFunction<T, Error, undefined | Args, unknown> = mutation.mutateAsync;
Example
const { mutate: updateTodo, isFetching, error } = useMutation<User>('todos.update');
// Later in your code:
updateTodo({ id: '123', name: 'New Name' });