Skip to content

Query that returns undefined is stuck in a fetching loop when used in child and parent component #4910

@philipfeldmann

Description

@philipfeldmann

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

  1. Open the console of codesandbox
  2. Click on the button "Break Stuff" Button
  3. 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions