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