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 {
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 )
41​
42function App() {
43 return (
44 <QueryClientProvider client={queryClient}>
45 <Example />
46 </QueryClientProvider>
47 )
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 )
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 )
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 )
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 )
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