Open History to see the last commit
1import { render } from 'preact'
2import { useState } from 'preact/hooks'
3import { QueryClient, useQuery, useQueryClient } from '@tanstack/preact-query'
4import { PersistQueryClientProvider } from '@tanstack/preact-query-persist-client'
5import { createAsyncStoragePersister } from '@tanstack/query-async-storage-persister'
6import { PreactQueryDevtools } from '@tanstack/preact-query-devtools'
7import { postsQueries } from './services/posts/queries'
8β
9const queryClient = new QueryClient({
10 defaultOptions: {
11 queries: {
12 gcTime: 24 * 60 * 60 * 1000, // 24 hours
13 },
14 },
15})
16β
17const persister = createAsyncStoragePersister({
18 storage: window.localStorage,
19})
20β
21function Posts({ setPostId }: { setPostId: (postId: number) => void }) {
22 const queryClient = useQueryClient()
23 const { status, data, error, isFetching } = useQuery(postsQueries.list())
24β
25 return (
26 <div>
27 <h1>Posts</h1>
28 <div>
29 {status === 'pending' ? (
30 'Loading...'
31 ) : status === 'error' ? (
32 <span>Error: {error.message}</span>
33 ) : (
34 <>
35 <div>
36 {data.map((post) => (
37 <p key={post.id}>
38 <a
39 onClick={() => setPostId(post.id)}
40 href="#"
41 style={
42 // We can access the query data here to show bold links for
43 // ones that are cached
44 queryClient.getQueryData(
45 postsQueries.detail(post.id).queryKey,
46 )
47 ? {
48 fontWeight: 'bold',
49 color: 'green',
50 }
51 : {}
52 }
53 >
54 {post.title}
55 </a>
56 </p>
57 ))}
58 </div>
59 <div>{isFetching ? 'Background Updating...' : ' '}</div>
60 </>
61 )}
62 </div>
63 </div>
64 )
65}
66β
67function Post({
68 postId,
69 setPostId,
70}: {
71 postId: number
72 setPostId: (postId: number) => void
73}) {
74 const { status, data, error, isFetching } = useQuery(
75 postsQueries.detail(postId),
76 )
77β
78 return (
79 <div>
80 <div>
81 <a onClick={() => setPostId(-1)} href="#">
82 Back
83 </a>
84 </div>
85 {status === 'pending' ? (
86 'Loading...'
87 ) : status === 'error' ? (
88 <span>Error: {error.message}</span>
89 ) : (
90 <>
91 <h1>{data.title}</h1>
92 <div>
93 <p>{data.body}</p>
94 </div>
95 <div>{isFetching ? 'Background Updating...' : ' '}</div>
96 </>
97 )}
98 </div>
99 )
100}
101β
102function App() {
103 const [postId, setPostId] = useState(-1)
104β
105 return (
106 <PersistQueryClientProvider
107 client={queryClient}
108 persistOptions={{ persister }}
109 >
110 <p>
111 As you visit the posts below, you will notice them in a loading state
112 the first time you load them. However, after you return to this list and
113 click on any posts you have already visited again, you will see them
114 load instantly and background refresh right before your eyes!{' '}
115 <strong>
116 (You may need to throttle your network speed to simulate longer
117 loading sequences)
118 </strong>
119 </p>
120 {postId > -1 ? (
121 <Post postId={postId} setPostId={setPostId} />
122 ) : (
123 <Posts setPostId={setPostId} />
124 )}
125 <PreactQueryDevtools initialIsOpen />
126 </PersistQueryClientProvider>
127 )
128}
129β
130const rootElement = document.getElementById('root')
131if (!rootElement) throw new Error('Missing #root element')
132render(<App />, rootElement)