Looking for the React Query v3 documentation?
Getting Started
Guides & Concepts
Community Resources
API Reference

Updates from Mutation Responses

When dealing with mutations that update objects on the server, it's common for the new object to be automatically returned in the response of the mutation. Instead of refetching any queries for that item and wasting a network call for data we already have, we can take advantage of the object returned by the mutation function and update the existing query with the new data immediately using the Query Client's setQueryData method:

const queryClient = useQueryClient()
const mutation = useMutation({
mutationFn: editTodo,
onSuccess: data => {
queryClient.setQueryData(['todo', { id: 5 }], data)
id: 5,
name: 'Do the laundry',
// The query below will be updated with the response from the
// successful mutation
const { status, data, error } = useQuery({
queryKey: ['todo', { id: 5 }],
queryFn: fetchTodoById,

You might want to tie the onSuccess logic into a reusable mutation, for that you can create a custom hook like this:

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)


Updates via setQueryData must be performed in an immutable way. DO NOT attempt to write directly to the cache by mutating data (that you retrieved via from the cache) in place. It might work at first but can lead to subtle bugs along the way.

['posts', { id }],
(oldData) => {
if (oldData) {
// ❌ do not try this
oldData.title = 'my new post title'
return oldData
['posts', { id }],
// ✅ this is the way
(oldData) => oldData ? {
title: 'my new post title'
} : oldData
Want to Skip the Docs?

Fast track your learning and
take the offical React Query course ↗️

Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.


No spam. Unsubscribe at any time.