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

solid-query.md

205 lines5.4 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(() => ['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 )
36​
37function App() {
38 return (
39 <QueryClientProvider client={queryClient}>
40 <Example />
41 </QueryClientProvider>
42 )
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 )
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 )
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 )
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 )
193​
194function App() {
195 return (
196 <QueryClientProvider client={queryClient}>
197 <Example />
198 </QueryClientProvider>
199 )
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