Skip to content

Commit 8223729

Browse files
committed
docs(react-query): undefined vs. null in query-functions.md
1 parent 14c0ec8 commit 8223729

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

docs/framework/react/guides/query-functions.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ title: Query Functions
55

66
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**.
77

8+
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.
9+
810
All of the following are valid query function configurations:
911

1012
[//]: # 'Example'
@@ -23,6 +25,13 @@ useQuery({
2325
queryKey: ['todos', todoId],
2426
queryFn: ({ queryKey }) => fetchTodoById(queryKey[1]),
2527
})
28+
useQuery({
29+
queryKey: ['todos', todoId],
30+
queryFn: async () => {
31+
const allTodosById = await fetchAllTodosById()
32+
return allTodosById[todoId] ?? null
33+
},
34+
})
2635
```
2736

2837
[//]: # 'Example'

0 commit comments

Comments
 (0)