Skip to content

Invalidation fails for query keys with null when a disabled query uses undefined in the key #9497

@mctep

Description

@mctep

Describe the bug

When a disabled query is registered with undefined in its queryKey, invalidation stops working for other queries that have null in the same key position.

This behavior is unexpected, since null and undefined should not be treated as equivalent or conflicting in this context.

Your minimal, reproducible example

https://codesandbox.io/p/sandbox/s3fw8s

Steps to reproduce

  1. Define a disabled query with undefined in the key:
useQuery({
  queryKey: ['query', undefined],
  enabled: false,
});
  1. Define an enabled query with null in the key:
useQuery({
  queryKey: ['query', null],
  queryFn: () => Math.random()
});
  1. Invalidate the second query:
useQueryClient().invalidateQueries({ queryKey: ['query', null] })

Expected behavior

The second query (with null in its key) should be invalidated and refetched.

Actual behavior
Nothing happens — the query is not invalidated or refetched.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: macOS Sequoia Version 15.5 (24F74)
  • Browser: Chrome
  • Version: 138.0.7204.101 (Official Build) (arm64)

Tanstack Query adapter

react-query

TanStack Query version

5.83.0

TypeScript version

4.4.4

Additional context

I suspected the issue might be related to how react-query serializes keys internally (e.g., via JSON.stringify). However, even if:

  • undefined is converted to null, both keys would be equivalent, and invalidation should still work (case 2 in CodeSandbox example).
  • undefined is omitted or transformed into a different form, keys should remain distinct and not interfere with each other (case 3 in CodeSandbox example).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions