G$RowG
tRPC2y ago
29 replies
G$Row

How to get data type from onMutate function in useMutation

I'm doing optimistic updates using the technique which is documented here https://tanstack.com/query/latest/docs/framework/react/guides/optimistic-updates . I found that I'm using the same queries in multiple spots and want to extract the update code so that I don't have to copy and paste. I can't figure out the type of the
data
parameter in the onMutate function.

api.task.createTask.useMutation({
    onMutate: async (data) => {}})

I've tried using type MutateData = Parameters<typeof api.task.createTask.useMutation>[0] . The problem is that I can't then access the ['onMutate'] because it doesn't exist on there. Does anyone know how to get this?
React Query provides two ways to optimistically update your UI before a mutation has completed. You can either use the onMutate option to update your cache directly, or leverage the returned variables to update your UI from the useMutation result.

Via the UI
Optimistic Updates | TanStack Query React Docs
Was this page helpful?