Capítulo 28 de 80
The idiomatic place to call invalidateQueries is a mutation's onSuccess callback — returning a Promise from it (e.g. via await/Promise.all) keeps the mutation's isPending true until the invalidation/refetch is actually complete, not just the write.
onSuccess rather than leaving it to chance/staleTime.Promise.all([queryClient.invalidateQueries({queryKey: ['todos']}), queryClient.invalidateQueries({queryKey: ['reminders']})]) invalidates several unrelated key groups from one mutation.onSuccess means mutation.isPending stays true until the refetch resolves too — useful when the UI shouldn't consider the operation "done" until fresh data has actually landed.onSuccess: the same queryClient invalidation calls can be wired into any of useMutation's other lifecycle callbacks (onSettled, etc.), not just onSuccess.const queryClient = useQueryClient()
const mutation = useMutation({
mutationFn: addTodo,
onSuccess: async () => {
await Promise.all([
queryClient.invalidateQueries({ queryKey: ['todos'] }),
queryClient.invalidateQueries({ queryKey: ['reminders'] }),
])
},
})
isPending correctly reflecting that the refetch is still in flight.onSuccess on the mutation itself (not on the caller's mutate() call) is the standard place for invalidation logic that should always run.