Open History to see the last commit
1/* eslint-disable jsx-a11y/anchor-is-valid */
2import React from "react";
3import ReactDOM from "react-dom";
4import axios from "axios";
5import {
6 useQuery,
7 useQueryClient,
8 QueryClient,
9 QueryClientProvider,
10} from "react-query";
11import { ReactQueryDevtools } from "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("posts", async () => {
42 await new Promise((r) => setTimeout(r, 2000));
43 const { data } = await axios.get(
44 "https://jsonplaceholder.typicode.com/posts"
45 );
46 return data;
47 });
48}
49β
50function Posts({ setPostId }) {
51 const queryClient = useQueryClient();
52 const { status, data, error, isFetching } = usePosts();
53β
54 return (
55 <div>
56 <h1>Posts</h1>
57 <div>
58 {status === "loading" ? (
59 "Loading..."
60 ) : status === "error" ? (
61 <span>Error: {error.message}</span>
62 ) : (
63 <>
64 <div>
65 {data.map((post) => (
66 <p key={post.id}>
67 <a
68 onClick={() => setPostId(post.id)}
69 href="#"
70 style={
71 // We can access the query data here to show bold links for
72 // ones that are cached
73 queryClient.getQueryData(["post", post.id])
74 ? {
75 fontWeight: "bold",
76 color: "green",
77 }
78 : {}
79 }
80 >
81 {post.title}
82 </a>
83 </p>
84 ))}
85 </div>
86 <div>{isFetching ? "Background Updating..." : " "}</div>
87 </>
88 )}
89 </div>
90 </div>
91 );
92}
93β
94const getPostById = async (id) => {
95 const { data } = await axios.get(
96 `https://jsonplaceholder.typicode.com/posts/${id}`
97 );
98 return data;
99};
100β
101function usePost(postId) {
102 return useQuery(["post", postId], () => getPostById(postId), {
103 enabled: !!postId,
104 });
105}
106β
107function Post({ postId, setPostId }) {
108 const { status, data, error, isFetching } = usePost(postId);
109β
110 return (
111 <div>
112 <div>
113 <a onClick={() => setPostId(-1)} href="#">
114 Back
115 </a>
116 </div>
117 {!postId || status === "loading" ? (
118 "Loading..."
119 ) : status === "error" ? (
120 <span>Error: {error.message}</span>
121 ) : (
122 <>
123 <h1>{data.title}</h1>
124 <div>
125 <p>{data.body}</p>
126 </div>
127 <div>{isFetching ? "Background Updating..." : " "}</div>
128 </>
129 )}
130 </div>
131 );
132}
133β
134const rootElement = document.getElementById("root");
135ReactDOM.render(<App />, rootElement);