|
1 | 1 | import { createRoot } from 'react-dom/client' |
| 2 | +import { QueryClient, QueryClientProvider, useQuery, useQueryClient } from '@tanstack/react-query' |
| 3 | +import { useState } from 'react' |
2 | 4 | import Devtools from './setup' |
3 | | -import { queryPlugin } from './plugin' |
4 | 5 |
|
5 | | -setTimeout(() => { |
6 | | - queryPlugin.emit('test', { |
7 | | - title: 'Test Event', |
8 | | - description: |
9 | | - 'This is a test event from the TanStack Query Devtools plugin.', |
10 | | - }) |
11 | | -}, 1000) |
12 | 6 |
|
13 | | -queryPlugin.on('test', (event) => { |
14 | | - console.log('Received test event:', event) |
| 7 | +const queryClient = new QueryClient({ |
| 8 | + defaultOptions: { |
| 9 | + queries: { |
| 10 | + gcTime: 1000 * 60 * 60 * 24, // 24 hours |
| 11 | + }, |
| 12 | + }, |
15 | 13 | }) |
16 | 14 |
|
| 15 | +type Post = { |
| 16 | + id: number |
| 17 | + title: string |
| 18 | + body: string |
| 19 | +} |
| 20 | + |
| 21 | +function Posts({ |
| 22 | + setPostId, |
| 23 | +}: { |
| 24 | + setPostId: React.Dispatch<React.SetStateAction<number>> |
| 25 | +}) { |
| 26 | + const queryClient = useQueryClient() |
| 27 | + const { status, data, error, isFetching } = usePosts() |
| 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(['post', post.id]) |
| 49 | + ? { |
| 50 | + fontWeight: 'bold', |
| 51 | + color: 'green', |
| 52 | + } |
| 53 | + : {} |
| 54 | + } |
| 55 | + > |
| 56 | + {post.title} |
| 57 | + </a> |
| 58 | + </p> |
| 59 | + ))} |
| 60 | + </div> |
| 61 | + <div>{isFetching ? 'Background Updating...' : ' '}</div> |
| 62 | + </> |
| 63 | + )} |
| 64 | + </div> |
| 65 | + </div> |
| 66 | + ) |
| 67 | +} |
| 68 | + |
| 69 | +const getPostById = async (id: number): Promise<Post> => { |
| 70 | + const response = await fetch( |
| 71 | + `https://jsonplaceholder.typicode.com/posts/${id}`, |
| 72 | + ) |
| 73 | + return await response.json() |
| 74 | +} |
| 75 | + |
| 76 | +function usePost(postId: number) { |
| 77 | + return useQuery({ |
| 78 | + queryKey: ['post', postId], |
| 79 | + queryFn: () => getPostById(postId), |
| 80 | + enabled: !!postId, |
| 81 | + }) |
| 82 | +} |
| 83 | + |
| 84 | +function Post({ |
| 85 | + postId, |
| 86 | + setPostId, |
| 87 | +}: { |
| 88 | + postId: number |
| 89 | + setPostId: React.Dispatch<React.SetStateAction<number>> |
| 90 | +}) { |
| 91 | + const { status, data, error, isFetching } = usePost(postId) |
| 92 | + |
| 93 | + return ( |
| 94 | + <div> |
| 95 | + <div> |
| 96 | + <a onClick={() => setPostId(-1)} href="#"> |
| 97 | + Back |
| 98 | + </a> |
| 99 | + </div> |
| 100 | + {!postId || status === 'pending' ? ( |
| 101 | + 'Loading...' |
| 102 | + ) : status === 'error' ? ( |
| 103 | + <span>Error: {error.message}</span> |
| 104 | + ) : ( |
| 105 | + <> |
| 106 | + <h1>{data.title}</h1> |
| 107 | + <div> |
| 108 | + <p>{data.body}</p> |
| 109 | + </div> |
| 110 | + <div>{isFetching ? 'Background Updating...' : ' '}</div> |
| 111 | + </> |
| 112 | + )} |
| 113 | + </div> |
| 114 | + ) |
| 115 | +} |
| 116 | +function usePosts() { |
| 117 | + return useQuery({ |
| 118 | + queryKey: ['posts'], |
| 119 | + queryFn: async (): Promise<Array<Post>> => { |
| 120 | + const response = await fetch('https://jsonplaceholder.typicode.com/posts') |
| 121 | + return await response.json() |
| 122 | + }, |
| 123 | + }) |
| 124 | +} |
17 | 125 | function App() { |
| 126 | + const [postId, setPostId] = useState(-1) |
| 127 | + |
18 | 128 | return ( |
19 | 129 | <div> |
| 130 | + <QueryClientProvider |
| 131 | + client={queryClient} |
| 132 | + > |
| 133 | + <p> |
| 134 | + As you visit the posts below, you will notice them in a loading state |
| 135 | + the first time you load them. However, after you return to this list and |
| 136 | + click on any posts you have already visited again, you will see them |
| 137 | + load instantly and background refresh right before your eyes!{' '} |
| 138 | + <strong> |
| 139 | + (You may need to throttle your network speed to simulate longer |
| 140 | + loading sequences) |
| 141 | + </strong> |
| 142 | + </p> |
| 143 | + {postId > -1 ? ( |
| 144 | + <Post postId={postId} setPostId={setPostId} /> |
| 145 | + ) : ( |
| 146 | + <Posts setPostId={setPostId} /> |
| 147 | + )} |
| 148 | + <Devtools /> |
| 149 | + </QueryClientProvider> |
20 | 150 | <h1>TanStack Devtools React Basic Example</h1> |
21 | | - <Devtools /> |
22 | 151 | </div> |
23 | 152 | ) |
24 | 153 | } |
|
0 commit comments