-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Description
Describe the bug
I just want to mention that I'm aware of the fact that undefined is no longer an allowed value for query results, but I think the behavior that results form that is still weird:
When having a parent component that uses a custom query hook that has a child which uses the same query hook and this hook returns undefined, the query will fetch over and over again, potentially resulting in thousands of requests.
Here's an example hook:
export const useUndefinedQuery = () => {
return useQuery(["some-query"], async () => {
// This would be an api call
await new Promise((resolve) => setTimeout(resolve, 100));
return undefined; // the result from the API call might be undefined
});
};
Please refer to my codesandbox example for a full example.
Your minimal, reproducible example
https://codesandbox.io/s/angry-benji-cc036g?file=/src/index.js
Steps to reproduce
- Open the console of codesandbox
- Click on the button "Break Stuff" Button
- The query will fetch over and over again - watch the console logs as the query has a console.log side-effect
Expected behavior
As a user, I expect the query to only load once. I understand that undefined is no longer a valid result of useQuery, but this infinite loop can lead to high costs when using services such as firebase, where undefined is returned regularly.
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
- macOS
- Chrome 109.0.5414.119
TanStack Query version
^4.22.0
TypeScript version
^4.6.4
Additional context
No response