File size: 6,026 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
---
id: quick-start
title: Quick Start
---
The `@tanstack/solid-query` package provides a 1st-class API for using TanStack Query with SolidJS.
## Example
```tsx
import {
QueryClient,
QueryClientProvider,
useQuery,
} from '@tanstack/solid-query'
import { Switch, Match, For } from 'solid-js'
const queryClient = new QueryClient()
function Example() {
const query = useQuery(() => ({
queryKey: ['todos'],
queryFn: fetchTodos,
}))
return (
<div>
<Switch>
<Match when={query.isPending}>
<p>Loading...</p>
</Match>
<Match when={query.isError}>
<p>Error: {query.error.message}</p>
</Match>
<Match when={query.isSuccess}>
<For each={query.data}>{(todo) => <p>{todo.title}</p>}</For>
</Match>
</Switch>
</div>
)
}
function App() {
return (
<QueryClientProvider client={queryClient}>
<Example />
</QueryClientProvider>
)
}
```
## Available Functions
Solid Query offers useful primitives and functions that will make managing server state in SolidJS apps easier.
- `useQuery`
- `createQueries`
- `createInfiniteQueries`
- `createMutation`
- `useIsFetching`
- `useIsMutating`
- `useQueryClient`
- `QueryClient`
- `QueryClientProvider`
## Important Differences between Solid Query & React Query
Solid Query offers an API similar to React Query, but there are some key differences to be mindful of.
- Arguments to `solid-query` primitives (like `useQuery`, `createMutation`, `useIsFetching`) listed above are functions, so that they can be tracked in a reactive scope.
```tsx
// β react version
useQuery({
queryKey: ['todos', todo],
queryFn: fetchTodos,
})
// β
solid version
useQuery(() => ({
queryKey: ['todos', todo],
queryFn: fetchTodos,
}))
```
- Suspense works for queries out of the box if you access the query data inside a `<Suspense>` boundary.
```tsx
import { For, Suspense } from 'solid-js'
function Example() {
const query = useQuery(() => ({
queryKey: ['todos'],
queryFn: fetchTodos,
}))
return (
<div>
{/* β
Will trigger loading fallback, data accessed in a suspense boundary. */}
<Suspense fallback={'Loading...'}>
<For each={query.data}>{(todo) => <div>{todo.title}</div>}</For>
</Suspense>
{/* β Will not trigger loading fallback, data not accessed in a suspense boundary. */}
<For each={query.data}>{(todo) => <div>{todo.title}</div>}</For>
</div>
)
}
```
- 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.
```tsx
import {
QueryClient,
QueryClientProvider,
useQuery,
} from '@tanstack/solid-query'
import { Match, Switch } from 'solid-js'
const queryClient = new QueryClient()
export default function App() {
return (
<QueryClientProvider client={queryClient}>
<Example />
</QueryClientProvider>
)
}
function Example() {
// β react version -- supports destructing outside reactive context
// const { isPending, error, data } = useQuery({
// queryKey: ['repoData'],
// queryFn: () =>
// fetch('https://api.github.com/repos/tannerlinsley/react-query').then(
// (res) => res.json()
// ),
// })
// β
solid version -- does not support destructuring outside reactive context
const query = useQuery(() => ({
queryKey: ['repoData'],
queryFn: () =>
fetch('https://api.github.com/repos/tannerlinsley/react-query').then(
(res) => res.json(),
),
}))
// β
access query properties in JSX reactive context
return (
<Switch>
<Match when={query.isPending}>Loading...</Match>
<Match when={query.isError}>Error: {query.error.message}</Match>
<Match when={query.isSuccess}>
<div>
<h1>{query.data.name}</h1>
<p>{query.data.description}</p>
<strong>π {query.data.subscribers_count}</strong>{' '}
<strong>β¨ {query.data.stargazers_count}</strong>{' '}
<strong>π΄ {query.data.forks_count}</strong>
</div>
</Match>
</Switch>
)
}
```
- Signals and store values can be passed in directly to function arguments. Solid Query will update the query `store` automatically.
```tsx
import {
QueryClient,
QueryClientProvider,
useQuery,
} from '@tanstack/solid-query'
import { createSignal, For } from 'solid-js'
const queryClient = new QueryClient()
function Example() {
const [enabled, setEnabled] = createSignal(false)
const [todo, setTodo] = createSignal(0)
// β
passing a signal directly is safe and observers update
// automatically when the value of a signal changes
const todosQuery = useQuery(() => ({
queryKey: ['todos'],
queryFn: fetchTodos,
enabled: enabled(),
}))
const todoDetailsQuery = useQuery(() => ({
queryKey: ['todo', todo()],
queryFn: fetchTodo,
enabled: todo() > 0,
}))
return (
<div>
<Switch>
<Match when={todosQuery.isPending}>
<p>Loading...</p>
</Match>
<Match when={todosQuery.isError}>
<p>Error: {todosQuery.error.message}</p>
</Match>
<Match when={todosQuery.isSuccess}>
<For each={todosQuery.data}>
{(todo) => (
<button onClick={() => setTodo(todo.id)}>{todo.title}</button>
)}
</For>
</Match>
</Switch>
<button onClick={() => setEnabled(!enabled())}>Toggle enabled</button>
</div>
)
}
function App() {
return (
<QueryClientProvider client={queryClient}>
<Example />
</QueryClientProvider>
)
}
```
- Errors can be caught and reset using SolidJS' native `ErrorBoundary` component.
Set `throwOnError` or the `suspense` option to `true` to make sure errors are thrown to the `ErrorBoundary`
- Since Property tracking is handled through Solid's fine grained reactivity, options like `notifyOnChangeProps` are not needed
|