|
| 1 | +import * as React from 'react' |
| 2 | +import ReactDOM from 'react-dom/client' |
| 3 | +import { QueryClient, useQuery, useQueryClient } from '@tanstack/react-query' |
| 4 | +import { PersistQueryClientProvider } from '@tanstack/react-query-persist-client' |
| 5 | +import { createSyncStoragePersister } from '@tanstack/query-sync-storage-persister' |
| 6 | +import { ReactQueryDevtools } from '@tanstack/react-query-devtools' |
| 7 | + |
| 8 | +const queryClient = new QueryClient({ |
| 9 | + defaultOptions: { |
| 10 | + queries: { |
| 11 | + gcTime: 1000 * 60 * 60 * 24, // 24 hours |
| 12 | + }, |
| 13 | + }, |
| 14 | +}) |
| 15 | + |
| 16 | +const persister = createSyncStoragePersister({ |
| 17 | + storage: window.localStorage, |
| 18 | +}) |
| 19 | + |
| 20 | +type Post = { |
| 21 | + id: number |
| 22 | + title: string |
| 23 | + body: string |
| 24 | +} |
| 25 | + |
| 26 | +function usePosts() { |
| 27 | + return useQuery({ |
| 28 | + queryKey: ['posts'], |
| 29 | + queryFn: async (): Promise<Array<Post>> => { |
| 30 | + const response = await fetch('https://jsonplaceholder.typicode.com/posts') |
| 31 | + return await response.json() |
| 32 | + }, |
| 33 | + }) |
| 34 | +} |
| 35 | + |
| 36 | +function Posts({ |
| 37 | + setPostId, |
| 38 | +}: { |
| 39 | + setPostId: React.Dispatch<React.SetStateAction<number>> |
| 40 | +}) { |
| 41 | + const queryClient = useQueryClient() |
| 42 | + const { status, data, error, isFetching } = usePosts() |
| 43 | + |
| 44 | + return ( |
| 45 | + <div> |
| 46 | + <h1>Posts</h1> |
| 47 | + <div> |
| 48 | + {status === 'pending' ? ( |
| 49 | + 'Loading...' |
| 50 | + ) : status === 'error' ? ( |
| 51 | + <span>Error: {error.message}</span> |
| 52 | + ) : ( |
| 53 | + <> |
| 54 | + <div> |
| 55 | + {data.map((post) => ( |
| 56 | + <p key={post.id}> |
| 57 | + <a |
| 58 | + onClick={() => setPostId(post.id)} |
| 59 | + href="#" |
| 60 | + style={ |
| 61 | + // We can access the query data here to show bold links for |
| 62 | + // ones that are cached |
| 63 | + queryClient.getQueryData(['post', post.id]) |
| 64 | + ? { |
| 65 | + fontWeight: 'bold', |
| 66 | + color: 'green', |
| 67 | + } |
| 68 | + : {} |
| 69 | + } |
| 70 | + > |
| 71 | + {post.title} |
| 72 | + </a> |
| 73 | + </p> |
| 74 | + ))} |
| 75 | + </div> |
| 76 | + <div>{isFetching ? 'Background Updating...' : ' '}</div> |
| 77 | + </> |
| 78 | + )} |
| 79 | + </div> |
| 80 | + </div> |
| 81 | + ) |
| 82 | +} |
| 83 | + |
| 84 | +const getPostById = async (id: number): Promise<Post> => { |
| 85 | + const response = await fetch( |
| 86 | + `https://jsonplaceholder.typicode.com/posts/${id}`, |
| 87 | + ) |
| 88 | + return await response.json() |
| 89 | +} |
| 90 | + |
| 91 | +function usePost(postId: number) { |
| 92 | + return useQuery({ |
| 93 | + queryKey: ['post', postId], |
| 94 | + queryFn: () => getPostById(postId), |
| 95 | + enabled: !!postId, |
| 96 | + }) |
| 97 | +} |
| 98 | + |
| 99 | +function Post({ |
| 100 | + postId, |
| 101 | + setPostId, |
| 102 | +}: { |
| 103 | + postId: number |
| 104 | + setPostId: React.Dispatch<React.SetStateAction<number>> |
| 105 | +}) { |
| 106 | + const { status, data, error, isFetching } = usePost(postId) |
| 107 | + |
| 108 | + return ( |
| 109 | + <div> |
| 110 | + <div> |
| 111 | + <a onClick={() => setPostId(-1)} href="#"> |
| 112 | + Back |
| 113 | + </a> |
| 114 | + </div> |
| 115 | + {!postId || status === 'pending' ? ( |
| 116 | + 'Loading...' |
| 117 | + ) : status === 'error' ? ( |
| 118 | + <span>Error: {error.message}</span> |
| 119 | + ) : ( |
| 120 | + <> |
| 121 | + <h1>{data.title}</h1> |
| 122 | + <div> |
| 123 | + <p>{data.body}</p> |
| 124 | + </div> |
| 125 | + <div>{isFetching ? 'Background Updating...' : ' '}</div> |
| 126 | + </> |
| 127 | + )} |
| 128 | + </div> |
| 129 | + ) |
| 130 | +} |
| 131 | + |
| 132 | +function App() { |
| 133 | + const [postId, setPostId] = React.useState(-1) |
| 134 | + |
| 135 | + return ( |
| 136 | + <PersistQueryClientProvider |
| 137 | + client={queryClient} |
| 138 | + persistOptions={{ persister }} |
| 139 | + > |
| 140 | + <p> |
| 141 | + As you visit the posts below, you will notice them in a loading state |
| 142 | + the first time you load them. However, after you return to this list and |
| 143 | + click on any posts you have already visited again, you will see them |
| 144 | + load instantly and background refresh right before your eyes!{' '} |
| 145 | + <strong> |
| 146 | + (You may need to throttle your network speed to simulate longer |
| 147 | + loading sequences) |
| 148 | + </strong> |
| 149 | + </p> |
| 150 | + {postId > -1 ? ( |
| 151 | + <Post postId={postId} setPostId={setPostId} /> |
| 152 | + ) : ( |
| 153 | + <Posts setPostId={setPostId} /> |
| 154 | + )} |
| 155 | + <ReactQueryDevtools initialIsOpen /> |
| 156 | + </PersistQueryClientProvider> |
| 157 | + ) |
| 158 | +} |
| 159 | + |
| 160 | +const rootElement = document.getElementById('root') as HTMLElement |
| 161 | +ReactDOM.createRoot(rootElement).render(<App />) |
0 commit comments