createMutation allows you define a way to trigger a query-like function in
response to an action that can perform mutations to the data.
For the most part, if you have a signal-based query, there is no need for a mutation unless you are performing unique actions like form submissions.
Additionally, mutations allow you to define a list of querys to invalidate after the mutation has been called.
Mutations take a Promise to call when run and an optional options Object.
createMutation only defines a mutation and does not actually call it. To use the
mutation you must pass the returned value into the useMutation hook.
The value returned is an Obect containing the mutate method and the state of the
mutation defined.
createMutation accepts the following options:
| Params | Required | Description |
|---|---|---|
| mutator | true | The Promise-based function to call when the mutate is called. |
| options |
The options Objecct contains the following optional properties:
| Property | Type | Description |
|---|---|---|
invalidate | 'all' | ((data: T, variables: V) => unknown[]) | A list of query.key to invalidate once the mutation has completed. |
onSuccess |
createMutation returns a MutationTuple to be passed into useMutation:
| Index | Type | Description |
|---|---|---|
| 0 | (variables: V) => Promise<T> | A function used to trigger the mutation. |
| 1 |
false| An Object of conditional options for defined mutations. |
(data: T, variables: V) => void| A callback to run when the mutation is successful. |
onError | (error: unknown, variables: V) => void | A callback to run when the mutation has errored. |
Accessor<MutationState<T>>| The state of the mutation. |
{
"id": "",
"name": ""
}