Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/framework/react/guides/query-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ title: Query Functions

A query function can be literally any function that **returns a promise**. The promise that is returned should either **resolve the data** or **throw an error**.

On success, the resolved value may be anything **except `undefined`**. Queries that resolve to `undefined` will be [treated as failed](https://tanstack.com/query/latest/docs/framework/react/guides/migrating-to-react-query-4#undefined-is-an-illegal-cache-value-for-successful-queries). To store "nothing" as a successful result in the query cache, resolve `null` instead.

All of the following are valid query function configurations:

[//]: # 'Example'
Expand All @@ -23,6 +25,13 @@ useQuery({
queryKey: ['todos', todoId],
queryFn: ({ queryKey }) => fetchTodoById(queryKey[1]),
})
useQuery({
queryKey: ['todos', todoId],
queryFn: async () => {
const allTodosById = await fetchAllTodosById()
return allTodosById[todoId] ?? null
},
})
```

[//]: # 'Example'
Expand Down
Loading