Open History to see the last commit
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()
22}
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 )
54}
55β
56function Posts(props: { setPostId: Setter<number> }) {
57 // All you have to do now is pass a key!
58 const state = useQuery<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(['post', 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 )
102}
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 )
137}
138β
139render(() => <App />, document.getElementById('root') as HTMLElement)