useUpdate is a modified version of TanStack Query’s useMutation for update mutations.
It uses the update method from the dataProvider and supports optimistic updates, pessimistic updates, and undoable mutations.
Usage
Parameters
You can pass parameters either to the hook or to themutate/mutateAsync functions. Parameters passed to the mutation functions override those passed to the hook.
Resource name for API data interactions.
ID of the record to update. Can be a
string or number.Values for the mutation function - the data to update.
Determines when mutations are executed. Defaults to the global mutation mode.
pessimistic: Updates are applied after the server respondsoptimistic: Updates are applied immediately, rolled back on errorundoable: Updates are applied immediately with an option to undo
Duration in milliseconds to wait before executing the mutation when
mutationMode is undoable.Callback that provides a function to cancel the mutation when
mutationMode is undoable.Meta data for the dataProvider. Can be used to pass additional parameters to data provider methods.
If there is more than one
dataProvider, you should specify which one to use.Specify which queries should be invalidated after successful mutation. Options:
'list', 'many', 'detail', 'resourceAll', 'all'.Customize optimistic update logic for different query types.
successNotification
OpenNotificationParams | false | ((data, params) => OpenNotificationParams | false)
Success notification configuration. Set to
false to disable.errorNotification
OpenNotificationParams | false | ((error, params) => OpenNotificationParams | false)
Error notification configuration. Set to
false to disable.TanStack Query’s
useMutation options (excluding onMutate).Configuration for loading overtime behavior.
Return Values
Function to trigger the mutation.
Async version that returns a promise.
TanStack Query’s
useMutation return object.Loading overtime information.
Examples
Basic Usage
With Async/Await
Optimistic Updates
Undoable Updates
Pessimistic Updates (Default)
Custom Optimistic Update Logic
With Callbacks
Custom Invalidation
Disable Automatic Invalidation
With Meta Data
Pre-configured Hook
Type Parameters
TData- Result data type of the mutation. ExtendsBaseRecord.TError- Custom error type that extendsHttpError.TVariables- Type of the values/payload for the mutation.
FAQ
What's the difference between mutation modes?
What's the difference between mutation modes?
- Pessimistic: Updates the UI only after the server responds (safest)
- Optimistic: Updates the UI immediately, reverts on error (best UX)
- Undoable: Updates UI immediately with an option to cancel before server update (Gmail-style)
How do I disable optimistic updates for specific query types?
How do I disable optimistic updates for specific query types?
Use the
optimisticUpdateMap parameter:Can I update only specific fields?
Can I update only specific fields?
Yes, pass only the fields you want to update in
values. Most data providers will perform a partial update (PATCH):How do I handle validation errors?
How do I handle validation errors?
Validation errors are included in the error object: