Open History to see the last commit
1---
2id: query-options
3title: Query Options
4ref: docs/framework/react/guides/query-options.md
5replace:
6 {
7 '@tanstack/react-query': '@tanstack/solid-query',
8 'useMutationState[(]': 'useMutationState(() => ',
9 'useMutation[(]': 'useMutation(() => ',
10 'useQuery[(]': 'useQuery(() => ',
11 'useQueries[(]': 'useQueries(() => ',
12 'useInfiniteQuery[(]': 'useInfiniteQuery(() => ',
13 }
14---
15β
16[//]: # 'Example1'
17β
18```ts
19import { queryOptions } from '@tanstack/solid-query'
20β
21function groupOptions(id: number) {
22 return queryOptions({
23 queryKey: ['groups', id],
24 queryFn: () => fetchGroups(id),
25 staleTime: 5 * 1000,
26 })
27}
28β
29// usage:
30β
31useQuery(() => groupOptions(1))
32useQueries(() => ({
33 queries: [groupOptions(1), groupOptions(2)],
34}))
35queryClient.query(groupOptions(23))
36queryClient.setQueryData(groupOptions(42).queryKey, newGroups)
37```
38β
39[//]: # 'Example1'
40[//]: # 'SelectDescription'
41β
42You can still override some options at the component level. A very common and useful pattern is to create per-component `select` functions:
43β
44[//]: # 'SelectDescription'
45[//]: # 'Example2'
46β
47```ts
48// Type inference still works, so query.data will be the return type of select instead of queryFn
49β
50const groupQuery = useQuery(() => ({
51 ...groupOptions(1),
52 select: (data) => data.groupName,
53}))
54```
55β
56[//]: # 'Example2'