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

quick-start.md

53 lines1.2 KB

1---
2id: quick-start
3title: Quick Start
4ref: docs/framework/react/quick-start.md
5replace: { 'React': 'Vue', 'react-query': 'vue-query' }
6---
7​
8[//]: # 'Example'
9​
10If you're looking for a fully functioning example, please have a look at our [basic codesandbox example](./examples/basic)
11​
12```vue
13<script setup>
14import { useQueryClient, useQuery, useMutation } from '@tanstack/vue-query'
15​
16// Access QueryClient instance
17const queryClient = useQueryClient()
18​
19// Query
20const { isPending, isError, data, error } = useQuery({
21 queryKey: ['todos'],
22 queryFn: getTodos,
23})
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 })
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​
53[//]: # 'Example'