Open History to see the last commit
1import { render, createContext } from 'preact'
2import { useContext, useState, useEffect } from 'preact/hooks'
3import Devtools from './setup'
4import { queryPlugin } from './plugin'
5import { Button } from './button'
6import { Feature } from './feature'
7β
8type Post = {
9 id: number
10 title: string
11 body: string
12}
13β
14function Posts({ setPostId }: { setPostId: (id: number) => void }) {
15 const [posts, setPosts] = useState<Post[]>([])
16 const [loading, setLoading] = useState(true)
17 const [error, setError] = useState<Error | null>(null)
18β
19 useEffect(() => {
20 fetch('https://jsonplaceholder.typicode.com/posts')
21 .then((res) => res.json())
22 .then((data) => {
23 setPosts(data)
24 setLoading(false)
25 })
26 .catch((err) => {
27 setError(err)
28 setLoading(false)
29 })
30 }, [])
31β
32 return (
33 <div>
34 <h1>Posts</h1>
35 <div>
36 {loading ? (
37 'Loading...'
38 ) : error ? (
39 <span>Error: {error.message}</span>
40 ) : (
41 <>
42 <div>
43 {posts.map((post) => (
44 <p key={post.id}>
45 <a onClick={() => setPostId(post.id)} href="#">
46 {post.title}
47 </a>
48 </p>
49 ))}
50 </div>
51 </>
52 )}
53 </div>
54 </div>
55 )
56}
57β
58const getPostById = async (id: number): Promise<Post> => {
59 const response = await fetch(
60 `https://jsonplaceholder.typicode.com/posts/${id}`,
61 )
62 return await response.json()
63}
64β
65function Post({
66 postId,
67 setPostId,
68}: {
69 postId: number
70 setPostId: (id: number) => void
71}) {
72 const [post, setPost] = useState<Post | null>(null)
73 const [loading, setLoading] = useState(true)
74 const [error, setError] = useState<Error | null>(null)
75β
76 useEffect(() => {
77 if (postId > 0) {
78 setLoading(true)
79 getPostById(postId)
80 .then((data) => {
81 setPost(data)
82 setLoading(false)
83 })
84 .catch((err) => {
85 setError(err)
86 setLoading(false)
87 })
88 }
89 }, [postId])
90β
91 return (
92 <div>
93 <div>
94 <a onClick={() => setPostId(-1)} href="#">
95 Back
96 </a>
97 </div>
98 {!postId || loading ? (
99 'Loading...'
100 ) : error ? (
101 <span>Error: {error.message}</span>
102 ) : post ? (
103 <>
104 <h1>{post.title}</h1>
105 <div>
106 <p>{post.body}</p>
107 </div>
108 </>
109 ) : null}
110 </div>
111 )
112}
113β
114const Context = createContext<{
115 count: number
116 setCount: (count: number) => void
117}>({ count: 0, setCount: () => {} })
118β
119setTimeout(() => {
120 queryPlugin.emit('test', {
121 title: 'Test Event',
122 description:
123 'This is a test event from the TanStack Query Devtools plugin.',
124 })
125}, 1000)
126β
127queryPlugin.on('test', (event) => {
128 console.log('Received test event:', event)
129})
130β
131function Mounted() {
132 const c = useContext(Context)
133 console.log(c)
134 return (
135 <p
136 onClick={() => {
137 c.setCount(c.count + 1)
138 }}
139 >
140 {c.count}
141 <hr />
142 </p>
143 )
144}
145β
146function App() {
147 const [state, setState] = useState(1)
148 const [win, setWin] = useState<Window | null>(null)
149 const [postId, setPostId] = useState(-1)
150 return (
151 <div>
152 <Context.Provider value={{ count: state, setCount: setState }}>
153 <h1>TanStack Devtools Preact Basic Example</h1>
154 current count: {state}
155 <Button onClick={() => setState(state + 1)}>Click me</Button>
156 <Button onClick={() => setWin(window.open('', '', 'popup'))}>
157 Click me to open new window
158 </Button>
159 {win && render(<Mounted />, win.document.body)}
160 <Feature />
161 <p>
162 As you visit the posts below, you will notice them in a loading state
163 the first time you load them. However, after you return to this list
164 and click on any posts you have already visited again, you will see
165 them load instantly and background refresh right before your eyes!{' '}
166 <strong>
167 (You may need to throttle your network speed to simulate longer
168 loading sequences)
169 </strong>
170 </p>
171 {postId > -1 ? (
172 <Post postId={postId} setPostId={setPostId} />
173 ) : (
174 <Posts setPostId={setPostId} />
175 )}
176 <Devtools />
177 </Context.Provider>
178 </div>
179 )
180}
181β
182render(<App />, document.getElementById('root')!)