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({ queryKey: ['todos'], queryFn: getTodos });
24β
25// Mutation
26const mutation = useMutation({
27 mutationFn: postTodo,
28 onSuccess: () => {
29 // Invalidate and refetch
30 queryClient.invalidateQueries({ queryKey: ["todos"] });
31 },
32});
33β
34function onButtonClick() {
35 mutation.mutate({
36 id: Date.now(),
37 title: "Do Laundry",
38 });
39}
40</script>
41β
42<template>
43 <span v-if="isPending">Loading...</span>
44 <span v-else-if="isError">Error: {{ error.message }}</span>
45 <!-- We can assume by this point that `isSuccess === true` -->
46 <ul v-else>
47 <li v-for="todo in data" :key="todo.id">{{ todo.title }}</li>
48 </ul>
49 <button @click="onButtonClick">Add Todo</button>
50</template>
51```
52β
53These 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.