Updates from Mutation Responses

· abundance's blog


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:

 1const queryClient = useQueryClient()
 2
 3const mutation = useMutation({
 4  mutationFn: editTodo,
 5  onSuccess: (data) => {
 6    queryClient.setQueryData(['todo', { id: 5 }], data)
 7  },
 8})
 9
10mutation.mutate({
11  id: 5,
12  name: 'Do the laundry',
13})
14
15// The query below will be updated with the response from the
16// successful mutation
17const { status, data, error } = useQuery({
18  queryKey: ['todo', { id: 5 }],
19  queryFn: fetchTodoById,
20})

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

 1const useMutateTodo = () => {
 2  const queryClient = useQueryClient()
 3
 4  return useMutation({
 5    mutationFn: editTodo,
 6    // Notice the second argument is the variables object that the `mutate` function receives
 7    onSuccess: (data, variables) => {
 8      queryClient.setQueryData(['todo', { id: variables.id }], data)
 9    },
10  })
11}

Immutability #

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 from the cache) in place. It might work at first but can lead to subtle bugs along the way.

 1queryClient.setQueryData(['posts', { id }], (oldData) => {
 2  if (oldData) {
 3    // ❌ do not try this
 4    oldData.title = 'my new post title'
 5  }
 6  return oldData
 7})
 8
 9queryClient.setQueryData(
10  ['posts', { id }],
11  // ✅ this is the way
12  (oldData) =>
13    oldData
14      ? {
15          ...oldData,
16          title: 'my new post title',
17        }
18      : oldData,
19)