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