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

index.tsx

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