Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

solid-query.md

221 lines5.6 KB

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 )
39​
40function App() {
41 return (
42 <QueryClientProvider client={queryClient}>
43 <Example />
44 </QueryClientProvider>
45 )
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 )
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 )
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 )
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 )
209​
210function App() {
211 return (
212 <QueryClientProvider client={queryClient}>
213 <Example />
214 </QueryClientProvider>
215 )
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