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

Post.svelte

38 lines812 Bytes

1<script lang="ts">
2 import { createQuery } from '@tanstack/svelte-query'
3 import { postsQueries } from './services/posts/queries'
4​
5 type Props = {
6 postId: string
7 setPostId: (postId: string | null) => void
8 }
9​
10 let { postId, setPostId }: Props = $props()
11​
12 const query = createQuery(() => postsQueries.detail(postId))
13</script>
14​
15<div>
16 <div>
17 <a
18 href="#posts"
19 onclick={(e) => {
20 e.preventDefault()
21 setPostId(null)
22 }}
23 >
24 Back
25 </a>
26 </div>
27 {#if query.status === 'pending'}
28 Loading...
29 {:else if query.status === 'error'}
30 <span>Error: {query.error.message}</span>
31 {:else}
32 <h1>{query.data.title}</h1>
33 <div>
34 <p>{query.data.body}</p>
35 </div>
36 <div>{query.isFetching ? 'Background Updating...' : ' '}</div>
37 {/if}
38</div>