Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

invalidations-from-mutations.md

43 lines1.1 KB

class TodoItemComponent {
  mutation = injectMutation(() => ({
    mutationFn: postTodo,
  }))
}
import { injectMutation } from '@tanstack/angular-query-experimental'

export class TodosComponent {
  queryClient = injectQueryClient()

  // When this mutation succeeds, invalidate any queries with the `todos` or `reminders` query key
  mutation = injectMutation((client) => ({
    mutationFn: addTodo,
    onSuccess: () => {
      client.invalidateQueries({ queryKey: ['todos'] })
      client.invalidateQueries({ queryKey: ['reminders'] })

      // OR use the queryClient that is injected into the component
      // this.queryClient.invalidateQueries({ queryKey: ['todos'] })
    },
  }))
}

You can wire up your invalidations to happen using any of the callbacks available in the injectMutation function