Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

index.js

142 lines3.7 KB

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";
12import { persistQueryClient } from "react-query/persistQueryClient-experimental";
13import { createLocalStoragePersistor } from "react-query/createLocalStoragePersistor-experimental";
14​
15const queryClient = new QueryClient();
16​
17persistQueryClient({
18 queryClient,
19 persistor: createLocalStoragePersistor(),
20});
21​
22function App() {
23 const [postId, setPostId] = React.useState(-1);
24​
25 return (
26 <QueryClientProvider client={queryClient}>
27 <p>
28 As you visit the posts below, you will notice them in a loading state
29 the first time you load them. However, after you return to this list and
30 click on any posts you have already visited again, you will see them
31 load instantly and background refresh right before your eyes!{" "}
32 <strong>
33 (You may need to throttle your network speed to simulate longer
34 loading sequences)
35 </strong>
36 </p>
37 {postId > -1 ? (
38 <Post postId={postId} setPostId={setPostId} />
39 ) : (
40 <Posts setPostId={setPostId} />
41 )}
42 <ReactQueryDevtools initialIsOpen />
43 </QueryClientProvider>
44 );
46​
47function usePosts() {
48 return useQuery("posts", async () => {
49 await new Promise((r) => setTimeout(r, 2000));
50 const { data } = await axios.get(
51 "https://jsonplaceholder.typicode.com/posts"
52 );
53 return data;
54 });
56​
57function Posts({ setPostId }) {
58 const queryClient = useQueryClient();
59 const { status, data, error, isFetching } = usePosts();
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 access the query data 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 );
100​
101const getPostById = async (id) => {
102 const { data } = await axios.get(
103 `https://jsonplaceholder.typicode.com/posts/${id}`
104 );
105 return data;
106};
107​
108function usePost(postId) {
109 return useQuery(["post", postId], () => getPostById(postId), {
110 enabled: !!postId,
111 });
113​
114function Post({ postId, setPostId }) {
115 const { status, data, error, isFetching } = usePost(postId);
116​
117 return (
118 <div>
119 <div>
120 <a onClick={() => setPostId(-1)} href="#">
121 Back
122 </a>
123 </div>
124 {!postId || status === "loading" ? (
125 "Loading..."
126 ) : status === "error" ? (
127 <span>Error: {error.message}</span>
128 ) : (
129 <>
130 <h1>{data.title}</h1>
131 <div>
132 <p>{data.body}</p>
133 </div>
134 <div>{isFetching ? "Background Updating..." : " "}</div>
135 </>
136 )}
137 </div>
138 );
140​
141const rootElement = document.getElementById("root");
142ReactDOM.render(<App />, rootElement);