|
| 1 | +import { createRoot } from 'react-dom/client' |
| 2 | +import { createContext, useContext, useState } from 'react' |
| 3 | +import { createPortal } from 'react-dom' |
| 4 | +import { |
| 5 | + QueryClient, |
| 6 | + QueryClientProvider, |
| 7 | + useQuery, |
| 8 | + useQueryClient, |
| 9 | +} from '@tanstack/react-query' |
| 10 | +import Devtools from './setup' |
| 11 | +import { queryPlugin } from './plugin' |
| 12 | +import { Button } from './button' |
| 13 | +import { Feature } from './feature' |
| 14 | + |
| 15 | +const queryClient = new QueryClient({ |
| 16 | + defaultOptions: { |
| 17 | + queries: { |
| 18 | + gcTime: 1000 * 60 * 60 * 24, // 24 hours |
| 19 | + }, |
| 20 | + }, |
| 21 | +}) |
| 22 | + |
| 23 | +type Post = { |
| 24 | + id: number |
| 25 | + title: string |
| 26 | + body: string |
| 27 | +} |
| 28 | + |
| 29 | +function Posts({ |
| 30 | + setPostId, |
| 31 | +}: { |
| 32 | + setPostId: React.Dispatch<React.SetStateAction<number>> |
| 33 | +}) { |
| 34 | + const queryClient = useQueryClient() |
| 35 | + const { status, data, error, isFetching } = usePosts() |
| 36 | + |
| 37 | + return ( |
| 38 | + <div> |
| 39 | + <h1>Posts</h1> |
| 40 | + <div> |
| 41 | + {status === 'pending' ? ( |
| 42 | + 'Loading...' |
| 43 | + ) : status === 'error' ? ( |
| 44 | + <span>Error: {error.message}</span> |
| 45 | + ) : ( |
| 46 | + <> |
| 47 | + <div> |
| 48 | + {data.map((post) => ( |
| 49 | + <p key={post.id}> |
| 50 | + <a |
| 51 | + onClick={() => setPostId(post.id)} |
| 52 | + href="#" |
| 53 | + style={ |
| 54 | + // We can access the query data here to show bold links for |
| 55 | + // ones that are cached |
| 56 | + queryClient.getQueryData(['post', post.id]) |
| 57 | + ? { |
| 58 | + fontWeight: 'bold', |
| 59 | + color: 'green', |
| 60 | + } |
| 61 | + : {} |
| 62 | + } |
| 63 | + > |
| 64 | + {post.title} |
| 65 | + </a> |
| 66 | + </p> |
| 67 | + ))} |
| 68 | + </div> |
| 69 | + <div>{isFetching ? 'Background Updating...' : ' '}</div> |
| 70 | + </> |
| 71 | + )} |
| 72 | + </div> |
| 73 | + </div> |
| 74 | + ) |
| 75 | +} |
| 76 | + |
| 77 | +const getPostById = async (id: number): Promise<Post> => { |
| 78 | + const response = await fetch( |
| 79 | + `https://jsonplaceholder.typicode.com/posts/${id}`, |
| 80 | + ) |
| 81 | + return await response.json() |
| 82 | +} |
| 83 | + |
| 84 | +function usePost(postId: number) { |
| 85 | + return useQuery({ |
| 86 | + queryKey: ['post', postId], |
| 87 | + queryFn: () => getPostById(postId), |
| 88 | + enabled: !!postId, |
| 89 | + }) |
| 90 | +} |
| 91 | + |
| 92 | +function Post({ |
| 93 | + postId, |
| 94 | + setPostId, |
| 95 | +}: { |
| 96 | + postId: number |
| 97 | + setPostId: React.Dispatch<React.SetStateAction<number>> |
| 98 | +}) { |
| 99 | + const { status, data, error, isFetching } = usePost(postId) |
| 100 | + |
| 101 | + return ( |
| 102 | + <div> |
| 103 | + <div> |
| 104 | + <a onClick={() => setPostId(-1)} href="#"> |
| 105 | + Back |
| 106 | + </a> |
| 107 | + </div> |
| 108 | + {!postId || status === 'pending' ? ( |
| 109 | + 'Loading...' |
| 110 | + ) : status === 'error' ? ( |
| 111 | + <span>Error: {error.message}</span> |
| 112 | + ) : ( |
| 113 | + <> |
| 114 | + <h1>{data.title}</h1> |
| 115 | + <div> |
| 116 | + <p>{data.body}</p> |
| 117 | + </div> |
| 118 | + <div>{isFetching ? 'Background Updating...' : ' '}</div> |
| 119 | + </> |
| 120 | + )} |
| 121 | + </div> |
| 122 | + ) |
| 123 | +} |
| 124 | +function usePosts() { |
| 125 | + return useQuery({ |
| 126 | + queryKey: ['posts'], |
| 127 | + queryFn: async (): Promise<Array<Post>> => { |
| 128 | + const response = await fetch('https://jsonplaceholder.typicode.com/posts') |
| 129 | + return await response.json() |
| 130 | + }, |
| 131 | + }) |
| 132 | +} |
| 133 | + |
| 134 | +const Context = createContext<{ |
| 135 | + count: number |
| 136 | + setCount: (count: number) => void |
| 137 | +}>({ count: 0, setCount: () => {} }) |
| 138 | + |
| 139 | +setTimeout(() => { |
| 140 | + queryPlugin.emit('test', { |
| 141 | + title: 'Test Event', |
| 142 | + description: |
| 143 | + 'This is a test event from the TanStack Query Devtools plugin.', |
| 144 | + }) |
| 145 | +}, 1000) |
| 146 | + |
| 147 | +queryPlugin.on('test', (event) => { |
| 148 | + console.log('Received test event:', event) |
| 149 | +}) |
| 150 | + |
| 151 | +function Mounted() { |
| 152 | + const c = useContext(Context) |
| 153 | + console.log(c) |
| 154 | + return ( |
| 155 | + <p |
| 156 | + onClick={() => { |
| 157 | + c.setCount(c.count + 1) |
| 158 | + }} |
| 159 | + > |
| 160 | + {c.count} |
| 161 | + <hr /> |
| 162 | + </p> |
| 163 | + ) |
| 164 | +} |
| 165 | + |
| 166 | +function App() { |
| 167 | + const [state, setState] = useState(1) |
| 168 | + const [win, setWin] = useState<Window | null>(null) |
| 169 | + const [postId, setPostId] = useState(-1) |
| 170 | + return ( |
| 171 | + <div> |
| 172 | + <Context.Provider value={{ count: state, setCount: setState }}> |
| 173 | + <QueryClientProvider client={queryClient}> |
| 174 | + <h1>TanStack Devtools React Basic Example</h1> |
| 175 | + current count: {state} |
| 176 | + <Button onClick={() => setState(state + 1)}>Click me</Button> |
| 177 | + <Button onClick={() => setWin(window.open('', '', 'popup'))}> |
| 178 | + Click me to open new window |
| 179 | + </Button> |
| 180 | + {win && createPortal(<Mounted />, win.document.body)} |
| 181 | + <Feature /> |
| 182 | + <p> |
| 183 | + As you visit the posts below, you will notice them in a loading |
| 184 | + state the first time you load them. However, after you return to |
| 185 | + this list and click on any posts you have already visited again, you |
| 186 | + will see them load instantly and background refresh right before |
| 187 | + your eyes!{' '} |
| 188 | + <strong> |
| 189 | + (You may need to throttle your network speed to simulate longer |
| 190 | + loading sequences) |
| 191 | + </strong> |
| 192 | + </p> |
| 193 | + {postId > -1 ? ( |
| 194 | + <Post postId={postId} setPostId={setPostId} /> |
| 195 | + ) : ( |
| 196 | + <Posts setPostId={setPostId} /> |
| 197 | + )} |
| 198 | + <Devtools /> |
| 199 | + </QueryClientProvider> |
| 200 | + </Context.Provider> |
| 201 | + </div> |
| 202 | + ) |
| 203 | +} |
| 204 | + |
| 205 | +const root = createRoot(document.getElementById('root')!) |
| 206 | +root.render(<App />) |
0 commit comments