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