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