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

index.tsx

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