Files
upstream-tanstack-query/docs/framework/solid/guides/updates-from-mutation-responses.md
upstream-mirror 3a013ccdfe
Some checks failed
autofix.ci / autofix (push) Failing after 4s
Release / Release (push) Has been skipped
GitHub Actions Security Analysis / Run zizmor (push) Failing after 2s
snapshot of https://github.com/TanStack/query.git (main, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:13:08 +05:00

1.0 KiB

id, title, ref, replace
id title ref replace
updates-from-mutation-responses Updates from Mutation Responses docs/framework/react/guides/updates-from-mutation-responses.md
hook
function
const queryClient = useQueryClient()

const mutation = useMutation(() => ({
  mutationFn: editTodo,
  onSuccess: (data) => {
    queryClient.setQueryData(['todo', { id: 5 }], data)
  },
}))

mutation.mutate({
  id: 5,
  name: 'Do the laundry',
})

// The query below will be updated with the response from the
// successful mutation
const todoQuery = useQuery(() => ({
  queryKey: ['todo', { id: 5 }],
  queryFn: fetchTodoById,
}))
const useMutateTodo = () => {
  const queryClient = useQueryClient()

  return useMutation(() => ({
    mutationFn: editTodo,
    // Notice the second argument is the variables object that the `mutate` function receives
    onSuccess: (data, variables) => {
      queryClient.setQueryData(['todo', { id: variables.id }], data)
    },
  }))
}