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

Posts.svelte

66 lines1.4 KB

1<script lang="ts">
2 import { useQueryClient, createQuery } from '@tanstack/svelte-query'
3 import { getPosts } from './data'
4​
5 const client = useQueryClient()
6​
7 let limit = 10
8​
9 const posts = createQuery<
10 { id: number; title: string; body: string }[],
11 Error
12 >(() => ({
13 queryKey: ['posts', limit],
14 queryFn: () => getPosts(limit),
15 }))
16</script>
17​
18<div>
19 <div>
20 {#if posts.status === 'pending'}
21 <span>Loading...</span>
22 {:else if posts.status === 'error'}
23 <span>Error: {posts.error.message}</span>
24 {:else}
25 <ul>
26 {#each posts.data as post}
27 <article>
28 <a
29 href={`/${post.id}`}
30 style={// We can use the queryCache here to show bold links for
31 // ones that are cached
32 client.getQueryData(['post', post.id])
33 ? 'font-weight: bold; color: indianred'
34 : 'cursor: pointer'}
35 >
36 {post.title}
37 </a>
38 </article>
39 {/each}
40 </ul>
41 <pre
42 class={['updating-text', posts.isFetching && 'on']}
43 style="font-weight:700">Background Updating...</pre>
44 {/if}
45 </div>
46</div>
47​
48<style>
49 article {
50 text-align: left;
51 }
52 a {
53 display: block;
54 font-size: 1.5rem;
55 margin-bottom: 1rem;
56 }
57​
58 .updating-text {
59 color: transparent;
60 transition: all 0.3s ease;
61 }
62 .updating-text.on {
63 color: green;
64 transition: none;
65 }
66</style>