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 {
11 QueryClient,
12 QueryClientProvider,
13 createQuery,
14} from '@tanstack/solid-query'
15import { Switch, Match, For } from 'solid-js'
16β
17const queryClient = new QueryClient()
18β
19function Example() {
20 const query = createQuery({
21 queryKey: () => ['todos'],
22 queryFn: fetchTodos,
23 })
24β
25 return (
26 <div>
27 <Switch>
28 <Match when={query.isPending}>
29 <p>Loading...</p>
30 </Match>
31 <Match when={query.isError}>
32 <p>Error: {query.error.message}</p>
33 </Match>
34 <Match when={query.isSuccess}>
35 <For each={query.data}>{(todo) => <p>{todo.title}</p>}</For>
36 </Match>
37 </Switch>
38 </div>
39 )
40}
41β
42function App() {
43 return (
44 <QueryClientProvider client={queryClient}>
45 <Example />
46 </QueryClientProvider>
47 )
48}
49```
50β
51## Available Functions
52β
53Solid Query offers useful primitives and functions that will make managing server state in SolidJS apps easier.
54β
55- `createQuery`
56- `createQueries`
57- `createInfiniteQueries`
58- `createMutation`
59- `useIsFetching`
60- `useIsMutating`
61- `useQueryClient`
62- `QueryClient`
63- `QueryClientProvider`
64β
65## Important Differences between Solid Query & React Query
66β
67Solid Query offers an API similar to React Query, but there are some key differences to be mindful of.
68β
69- To maintain their reactivity, Query keys need to be wrapped inside a function while using `createQuery`, `createQueries`, `createInfiniteQuery` and `useIsFetching`.
70β
71```tsx
72// β react version
73useQuery({
74 queryKey: ['todos', todo],
75 queryFn: fetchTodos,
76})
77β
78// β
solid version
79createQuery({
80 queryKey: () => ['todos', todo()],
81 queryFn: fetchTodos,
82})
83```
84β
85- Suspense works for queries out of the box if you access the query data inside a `<Suspense>` boundary.
86β
87```tsx
88import { For, Suspense } from 'solid-js'
89β
90function Example() {
91 const query = createQuery({
92 queryKey: () => ['todos'],
93 queryFn: fetchTodos,
94 })
95β
96 return (
97 <div>
98 {/* β
Will trigger loading fallback, data accessed in a suspense context. */}
99 <Suspense fallback={'Loading...'}>
100 <For each={query.data}>{(todo) => <div>{todo.title}</div>}</For>
101 </Suspense>
102 {/* β Will not trigger loading fallback, data not accessed in a suspense context. */}
103 <For each={query.data}>{(todo) => <div>{todo.title}</div>}</For>
104 </div>
105 )
106}
107```
108β
109- 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.
110β
111```tsx
112import {
113 QueryClient,
114 QueryClientProvider,
115 createQuery,
116} from '@tanstack/solid-query'
117import { Match, Switch } from 'solid-js'
118β
119const queryClient = new QueryClient()
120β
121export default function App() {
122 return (
123 <QueryClientProvider client={queryClient}>
124 <Example />
125 </QueryClientProvider>
126 )
127}
128β
129function Example() {
130 // β react version -- supports destructing outside reactive context
131 // const { isPending, error, data } = useQuery({
132 // queryKey: ['repoData'], () =>
133 // queryFn: fetch('https://api.github.com/repos/tannerlinsley/react-query').then(res =>
134 // res.json()
135 // )
136 // })
137β
138 // β
solid version -- does not support destructuring outside reactive context
139 const query = createQuery({
140 queryKey: () => ['repoData'],
141 queryFn: () =>
142 fetch('https://api.github.com/repos/tannerlinsley/react-query').then(
143 (res) => res.json(),
144 ),
145 })
146β
147 // β
access query properties in JSX reactive context
148 return (
149 <Switch>
150 <Match when={query.isPending}>Loading...</Match>
151 <Match when={query.isError}>Error: {query.error.message}</Match>
152 <Match when={query.isSuccess}>
153 <div>
154 <h1>{query.data.name}</h1>
155 <p>{query.data.description}</p>
156 <strong>π {query.data.subscribers_count}</strong>{' '}
157 <strong>β¨ {query.data.stargazers_count}</strong>{' '}
158 <strong>π΄ {query.data.forks_count}</strong>
159 </div>
160 </Match>
161 </Switch>
162 )
163}
164```
165β
166- 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.
167β
168```tsx
169import {
170 QueryClient,
171 QueryClientProvider,
172 createQuery,
173} from '@tanstack/solid-query'
174import { createSignal, For } from 'solid-js'
175β
176const queryClient = new QueryClient()
177β
178function Example() {
179 const [enabled, setEnabled] = createSignal(false)
180 const query = createQuery({
181 queryKey: () => ['todos'],
182 queryFn: fetchTodos,
183 // β passing a signal directly is not reactive
184 // enabled: enabled(),
185β
186 // β
passing a function that returns a signal is reactive
187 get enabled() {
188 return enabled()
189 },
190 })
191β
192 return (
193 <div>
194 <Switch>
195 <Match when={query.isPending}>
196 <p>Loading...</p>
197 </Match>
198 <Match when={query.isError}>
199 <p>Error: {query.error.message}</p>
200 </Match>
201 <Match when={query.isSuccess}>
202 <For each={query.data}>{(todo) => <p>{todo.title}</p>}</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