snapshot of https://github.com/TanStack/query.git (main, 2026-09-16; blobs > 131072B omitted)
This commit is contained in:
81
docs/framework/solid/guides/query-functions.md
Normal file
81
docs/framework/solid/guides/query-functions.md
Normal file
@@ -0,0 +1,81 @@
|
||||
---
|
||||
id: query-functions
|
||||
title: Query Functions
|
||||
ref: docs/framework/react/guides/query-functions.md
|
||||
---
|
||||
|
||||
[//]: # 'Example'
|
||||
|
||||
```tsx
|
||||
useQuery(() => ({ queryKey: ['todos'], queryFn: fetchAllTodos }))
|
||||
useQuery(() => ({
|
||||
queryKey: ['todos', todoId],
|
||||
queryFn: () => fetchTodoById(todoId),
|
||||
}))
|
||||
useQuery(() => ({
|
||||
queryKey: ['todos', todoId],
|
||||
queryFn: async () => {
|
||||
const data = await fetchTodoById(todoId)
|
||||
return data
|
||||
},
|
||||
}))
|
||||
useQuery(() => ({
|
||||
queryKey: ['todos', todoId],
|
||||
queryFn: ({ queryKey }) => fetchTodoById(queryKey[1]),
|
||||
}))
|
||||
```
|
||||
|
||||
[//]: # 'Example'
|
||||
[//]: # 'Example2'
|
||||
|
||||
```tsx
|
||||
const todosQuery = useQuery(() => ({
|
||||
queryKey: ['todos', todoId],
|
||||
queryFn: async () => {
|
||||
if (somethingGoesWrong) {
|
||||
throw new Error('Oh no!')
|
||||
}
|
||||
if (somethingElseGoesWrong) {
|
||||
return Promise.reject(new Error('Oh no!'))
|
||||
}
|
||||
|
||||
return data
|
||||
},
|
||||
}))
|
||||
```
|
||||
|
||||
[//]: # 'Example2'
|
||||
[//]: # 'Example3'
|
||||
|
||||
```tsx
|
||||
useQuery(() => ({
|
||||
queryKey: ['todos', todoId],
|
||||
queryFn: async () => {
|
||||
const response = await fetch('/todos/' + todoId)
|
||||
if (!response.ok) {
|
||||
throw new Error('Network response was not ok')
|
||||
}
|
||||
return response.json()
|
||||
},
|
||||
}))
|
||||
```
|
||||
|
||||
[//]: # 'Example3'
|
||||
[//]: # 'Example4'
|
||||
|
||||
```tsx
|
||||
function Todos(props) {
|
||||
const todosQuery = useQuery(() => ({
|
||||
queryKey: ['todos', { status: props.status, page: props.page }],
|
||||
queryFn: fetchTodoList,
|
||||
}))
|
||||
}
|
||||
|
||||
// Access the key, status and page variables in your query function!
|
||||
function fetchTodoList({ queryKey }) {
|
||||
const [_key, { status, page }] = queryKey
|
||||
return new Promise()
|
||||
}
|
||||
```
|
||||
|
||||
[//]: # 'Example4'
|
||||
Reference in New Issue
Block a user