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

CharacterItem.vue

38 lines1013 Bytes

1<script setup lang="ts">
2import { noop, useQuery, useQueryClient } from '@tanstack/vue-query'
3import { charactersQueries } from '../services/characters/queries'
4import type { Character } from '../services/characters/types'
5​
6const props = defineProps<{ char: Character }>()
7const emit = defineEmits<{ select: [id: number] }>()
8​
9const queryClient = useQueryClient()
10​
11const { data } = useQuery(() => ({
12 ...charactersQueries.detail(props.char.id),
13 enabled: false,
14}))
15​
16const prefetch = () => {
17 queryClient
18 .query({
19 ...charactersQueries.detail(props.char.id),
20 staleTime: 10 * 1000, // only prefetch if older than 10 seconds
21 })
22 .catch(noop)
24</script>
25​
26<template>
27 <li @mouseenter="prefetch" @focus="prefetch">
28 <button
29 type="button"
30 :style="data ? { fontWeight: 'bold' } : {}"
31 @click="emit('select', char.id)"
32 >
33 {{ char.id }} - {{ char.name }}
34 </button>
35 {{ ' ' }}
36 <NuxtLink :to="`/character/${char.id}`">details</NuxtLink>
37 </li>
38</template>