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

vue-query.md

56 lines1.5 KB

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 })
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.