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β
13// Define a default query function that will receive the query key
14const defaultQueryFn = async ({ queryKey }) => {
15 const { data } = await axios.get(
16 `https://jsonplaceholder.typicode.com${queryKey[0]}`
17 );
18 return data;
19};
20β
21// provide the default query function to your app via the query client
22const queryClient = new QueryClient({
23 defaultOptions: {
24 queries: {
25 queryFn: defaultQueryFn,
26 },
27 },
28});
29β
30function App() {
31 const [postId, setPostId] = React.useState(-1);
32β
33 return (
34 <QueryClientProvider client={queryClient}>
35 <p>
36 As you visit the posts below, you will notice them in a loading state
37 the first time you load them. However, after you return to this list and
38 click on any posts you have already visited again, you will see them
39 load instantly and background refresh right before your eyes!{" "}
40 <strong>
41 (You may need to throttle your network speed to simulate longer
42 loading sequences)
43 </strong>
44 </p>
45 {postId > -1 ? (
46 <Post postId={postId} setPostId={setPostId} />
47 ) : (
48 <Posts setPostId={setPostId} />
49 )}
50 <ReactQueryDevtools initialIsOpen />
51 </QueryClientProvider>
52 );
53}
54β
55function Posts({ setPostId }) {
56 const queryClient = useQueryClient();
57β
58 // All you have to do now is pass a key!
59 const { status, data, error, isFetching } = useQuery("/posts");
60β
61 return (
62 <div>
63 <h1>Posts</h1>
64 <div>
65 {status === "loading" ? (
66 "Loading..."
67 ) : status === "error" ? (
68 <span>Error: {error.message}</span>
69 ) : (
70 <>
71 <div>
72 {data.map((post) => (
73 <p key={post.id}>
74 <a
75 onClick={() => setPostId(post.id)}
76 href="#"
77 style={
78 // We can use the queryCache here to show bold links for
79 // ones that are cached
80 queryClient.getQueryData(["post", post.id])
81 ? {
82 fontWeight: "bold",
83 color: "green",
84 }
85 : {}
86 }
87 >
88 {post.title}
89 </a>
90 </p>
91 ))}
92 </div>
93 <div>{isFetching ? "Background Updating..." : " "}</div>
94 </>
95 )}
96 </div>
97 </div>
98 );
99}
100β
101function Post({ postId, setPostId }) {
102 // You can even leave out the queryFn and just go straight into options
103 const { status, data, error, isFetching } = useQuery(`/posts/${postId}`, {
104 enabled: !!postId,
105 });
106β
107 return (
108 <div>
109 <div>
110 <a onClick={() => setPostId(-1)} href="#">
111 Back
112 </a>
113 </div>
114 {!postId || status === "loading" ? (
115 "Loading..."
116 ) : status === "error" ? (
117 <span>Error: {error.message}</span>
118 ) : (
119 <>
120 <h1>{data.title}</h1>
121 <div>
122 <p>{data.body}</p>
123 </div>
124 <div>{isFetching ? "Background Updating..." : " "}</div>
125 </>
126 )}
127 </div>
128 );
129}
130β
131const rootElement = document.getElementById("root");
132ReactDOM.render(<App />, rootElement);