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