본문으로 건너뛰기

뮤테이션 응답을 통한 업데이트

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)
},
}))
}