Open History to see the last commit
1---
2title: Solid Query
3---
4β
5The `@tanstack/solid-query` package provides a 1st-class API for using TanStack Query with SolidJS.
6β
7## Example
8β
9```tsx
10import { QueryClient, QueryClientProvider, createQuery } from '@tanstack/solid-query'
11import { Switch, Match, For } from 'solid-js'
12β
13const queryClient = new QueryClient()
14β
15function Example() {
16 const query = createQuery({
17 queryKey: () => ['todos'],
18 queryFn: fetchTodos
19 })
20β
21 return (
22 <div>
23 <Switch>
24 <Match when={query.isPending}>
25 <p>Loading...</p>
26 </Match>
27 <Match when={query.isError}>
28 <p>Error: {query.error.message}</p>
29 </Match>
30 <Match when={query.isSuccess}>
31 <For each={query.data}>
32 {(todo) => <p>{todo.title}</p>}
33 </For>
34 </Match>
35 </Switch>
36 </div>
37 )
38}
39β
40function App() {
41 return (
42 <QueryClientProvider client={queryClient}>
43 <Example />
44 </QueryClientProvider>
45 )
46}
47β
48```
49β
50## Available Functions
51β
52Solid Query offers useful primitives and functions that will make managing server state in SolidJS apps easier.
53β
54- `createQuery`
55- `createQueries`
56- `createInfiniteQueries`
57- `createMutation`
58- `useIsFetching`
59- `useIsMutating`
60- `useQueryClient`
61- `QueryClient`
62- `QueryClientProvider`
63β
64β
65β
66β
67## Important Differences between Solid Query & React Query
68β
69Solid Query offers an API similar to React Query, but there are some key differences to be mindful of.
70β
71- To maintain their reactivity, Query keys need to be wrapped inside a function while using `createQuery`, `createQueries`, `createInfiniteQuery` and `useIsFetching`.
72β
73```tsx
74// β react version
75useQuery({
76 queryKey: ["todos", todo],
77 queryFn: fetchTodos
78})
79β
80// β
solid version
81createQuery({
82 queryKey: () => ["todos", todo()],
83 queryFn: fetchTodos
84})
85```
86β
87- Suspense works for queries out of the box if you access the query data inside a `<Suspense>` boundary.
88β
89```tsx
90import { For, Suspense } from 'solid-js'
91β
92function Example() {
93 const query = createQuery({
94 queryKey: () => ['todos'],
95 queryFn: fetchTodos
96 })
97β
98 return (
99 <div>
100 {/* β
Will trigger loading fallback, data accessed in a suspense context. */}
101 <Suspense fallback={"Loading..."}>
102 <For each={query.data}>{(todo) => <div>{todo.title}</div>}</For>
103 </Suspense>
104 {/* β Will not trigger loading fallback, data not accessed in a suspense context. */}
105 <For each={query.data}>{(todo) => <div>{todo.title}</div>}</For>
106 </div>
107 )
108}
109```
110β
111- Solid Query primitives (`createX`) do not support destructuring. The return value from these functions is a store, and their properties are only tracked in a reactive context.
112β
113```tsx
114import { QueryClient, QueryClientProvider, createQuery } from '@tanstack/solid-query'
115import { Match, Switch } from 'solid-js'
116β
117const queryClient = new QueryClient()
118β
119export default function App() {
120 return (
121 <QueryClientProvider client={queryClient}>
122 <Example />
123 </QueryClientProvider>
124 )
125}
126β
127function Example() {
128 // β react version -- supports destructing outside reactive context
129 // const { isPending, error, data } = useQuery({
130 // queryKey: ['repoData'], () =>
131 // queryFn: fetch('https://api.github.com/repos/tannerlinsley/react-query').then(res =>
132 // res.json()
133 // )
134 // })
135β
136 // β
solid version -- does not support destructuring outside reactive context
137 const query = createQuery({
138 queryKey: () => ['repoData'],
139 queryFn: () =>
140 fetch('https://api.github.com/repos/tannerlinsley/react-query').then(
141 (res) => res.json(),
142 ),
143 })
144β
145 // β
access query properties in JSX reactive context
146 return (
147 <Switch>
148 <Match when={query.isPending}>Loading...</Match>
149 <Match when={query.isError}>Error: {query.error.message}</Match>
150 <Match when={query.isSuccess}>
151 <div>
152 <h1>{query.data.name}</h1>
153 <p>{query.data.description}</p>
154 <strong>π {query.data.subscribers_count}</strong>{' '}
155 <strong>β¨ {query.data.stargazers_count}</strong>{' '}
156 <strong>π΄ {query.data.forks_count}</strong>
157 </div>
158 </Match>
159 </Switch>
160 )
161}
162```
163β
164- If you want options to be reactive you need to pass them using object getter syntax. This may look strange at first but it leads to more idiomatic solid code.
165β
166```tsx
167import {
168 QueryClient,
169 QueryClientProvider,
170 createQuery,
171} from '@tanstack/solid-query'
172import { createSignal, For } from 'solid-js'
173β
174const queryClient = new QueryClient()
175β
176function Example() {
177 const [enabled, setEnabled] = createSignal(false)
178 const query = createQuery({
179 queryKey: () => ['todos'],
180 queryFn: fetchTodos,
181 // β passing a signal directly is not reactive
182 // enabled: enabled(),
183β
184 // β
passing a function that returns a signal is reactive
185 get enabled() {
186 return enabled()
187 },
188 })
189β
190 return (
191 <div>
192 <Switch>
193 <Match when={query.isPending}>
194 <p>Loading...</p>
195 </Match>
196 <Match when={query.isError}>
197 <p>Error: {query.error.message}</p>
198 </Match>
199 <Match when={query.isSuccess}>
200 <For each={query.data}>
201 {(todo) => <p>{todo.title}</p>}
202 </For>
203 </Match>
204 </Switch>
205 <button onClick={() => setEnabled(!enabled())}>Toggle enabled</button>
206 </div>
207 )
208}
209β
210function App() {
211 return (
212 <QueryClientProvider client={queryClient}>
213 <Example />
214 </QueryClientProvider>
215 )
216}
217```
218β
219- Errors can be caught and reset using SolidJS' native `ErrorBoundary` component. `QueryErrorResetBoundary` is not needed with Solid Query
220β
221- Since Property tracking is handled through Solid's fine grained reactivity, options like `notifyOnChangeProps` are not needed