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

index.tsx

127 lines3.7 KB

1/* @refresh reload */
2import {
3 QueryClient,
4 QueryClientProvider,
5 useQuery,
6} from '@tanstack/solid-query'
7import { SolidQueryDevtools } from '@tanstack/solid-query-devtools'
8import { For, Match, Switch, createSignal } from 'solid-js'
9import { render } from 'solid-js/web'
10import { postsQueries } from './services/posts/queries'
11import type { Component, Setter } from 'solid-js'
12​
13const queryClient = new QueryClient({
14 defaultOptions: {
15 queries: {
16 gcTime: 24 * 60 * 60 * 1000, // 24 hours
17 },
18 },
19})
20​
21function Posts(props: { setPostId: Setter<number> }) {
22 const state = useQuery(() => postsQueries.list())
23​
24 return (
25 <div>
26 <h1>Posts</h1>
27 <div>
28 <Switch>
29 <Match when={state.status === 'pending'}>Loading...</Match>
30 <Match when={state.status === 'error'}>
31 <span>Error: {(state.error as Error).message}</span>
32 </Match>
33 <Match when={state.data !== undefined}>
34 <>
35 <div>
36 <For each={state.data}>
37 {(post) => (
38 <p>
39 <a
40 onClick={() => props.setPostId(post.id)}
41 href="#"
42 style={
43 // We can access the query data here to show bold links for
44 // ones that are cached
45 queryClient.getQueryData(
46 postsQueries.detail(post.id).queryKey,
47 )
48 ? {
49 'font-weight': 'bold',
50 color: 'green',
51 }
52 : {}
53 }
54 >
55 {post.title}
56 </a>
57 </p>
58 )}
59 </For>
60 </div>
61 <div>{state.isFetching ? 'Background Updating...' : ' '}</div>
62 </>
63 </Match>
64 </Switch>
65 </div>
66 </div>
67 )
69​
70function Post(props: { postId: number; setPostId: Setter<number> }) {
71 const state = useQuery(() => postsQueries.detail(props.postId))
72​
73 return (
74 <div>
75 <div>
76 <a onClick={() => props.setPostId(-1)} href="#">
77 Back
78 </a>
79 </div>
80 <Switch>
81 <Match when={state.status === 'pending'}>Loading...</Match>
82 <Match when={state.status === 'error'}>
83 <span>Error: {(state.error as Error).message}</span>
84 </Match>
85 <Match when={state.data !== undefined}>
86 <>
87 <h1>{state.data?.title}</h1>
88 <div>
89 <p>{state.data?.body}</p>
90 </div>
91 <div>{state.isFetching ? 'Background Updating...' : ' '}</div>
92 </>
93 </Match>
94 </Switch>
95 </div>
96 )
98​
99const App: Component = () => {
100 const [postId, setPostId] = createSignal(-1)
101​
102 return (
103 <QueryClientProvider client={queryClient}>
104 <SolidQueryDevtools />
105 <p>
106 As you visit the posts below, you will notice them in a loading state
107 the first time you load them. However, after you return to this list and
108 click on any posts you have already visited again, you will see them
109 load instantly and background refresh right before your eyes!{' '}
110 <strong>
111 (You may need to throttle your network speed to simulate longer
112 loading sequences)
113 </strong>
114 </p>
115 {postId() > -1 ? (
116 <Post postId={postId()} setPostId={setPostId} />
117 ) : (
118 <Posts setPostId={setPostId} />
119 )}
120 </QueryClientProvider>
121 )
123​
124const root = document.getElementById('root')
125if (!root) throw new Error('Missing #root element')
126​
127render(() => <App />, root)