Open History to see the last commit
1---
2title: Vue Query
3---
4β
5The `vue-query` package offers a 1st-class API for using TanStack Query via Vue. However, all of the primitives you receive from these hooks are core APIs that are shared across all of the TanStack Adapters including the Query Client, query results, query subscriptions, etc.
6β
7## Example
8β
9This example very briefly illustrates the 3 core concepts of Vue Query:
10β
11- [Queries](guides/queries)
12- [Mutations](guides/mutations)
13- [Query Invalidation](guides/query-invalidation)
14β
15```vue
16<script setup>
17import { useQueryClient, useQuery, useMutation } from '@tanstack/vue-query'
18β
19// Access QueryClient instance
20const queryClient = useQueryClient()
21β
22// Query
23const { isPending, isError, data, error } = useQuery({
24 queryKey: ['todos'],
25 queryFn: getTodos,
26})
27β
28// Mutation
29const mutation = useMutation({
30 mutationFn: postTodo,
31 onSuccess: () => {
32 // Invalidate and refetch
33 queryClient.invalidateQueries({ queryKey: ['todos'] })
34 },
35})
36β
37function onButtonClick() {
38 mutation.mutate({
39 id: Date.now(),
40 title: 'Do Laundry',
41 })
42}
43</script>
44β
45<template>
46 <span v-if="isPending">Loading...</span>
47 <span v-else-if="isError">Error: {{ error.message }}</span>
48 <!-- We can assume by this point that `isSuccess === true` -->
49 <ul v-else>
50 <li v-for="todo in data" :key="todo.id">{{ todo.title }}</li>
51 </ul>
52 <button @click="onButtonClick">Add Todo</button>
53</template>
54```
55β
56These three concepts make up most of the core functionality of Vue Query. The next sections of the documentation will go over each of these core concepts in great detail.