← Trail· Lesson 7 of 8
Mutations with React Query
Create/update data and invalidate the cache.
TODO i18n: Full body translation pending. Title and description are localized; the body below remains in Portuguese for now.
useMutation cobre POST/PUT/DELETE. No sucesso, invalide queries relacionadas para refetch.
const mutation = useMutation({
mutationFn: async (title: string) => {
/* POST ... */
return { id: Date.now(), title };
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['todos'] });
},
});