Skip to content

useQueries returns results in incorrect order when duplicate unsorted queries exist #8352

@jmtoung

Description

@jmtoung

Describe the bug

There is a bug in the returned value of useQueries when there are duplicate, unsorted queries, such as useQueries([ { queryKey: ['A'] }, { queryKey: ['B'] }, { queryKey: ['A'] }]. The current code returns [ QueryResult<'A'>, QueryResult<'A'>, QueryResult<'B'> ].

The root cause is due to a bug introduced here in the QueriesObserver's #findMatchingObservers method, where we always sort the results based on the queryHash.

Your minimal, reproducible example

https://codesandbox.io/p/sandbox/dreamy-matsumoto-mc8jtm

Steps to reproduce

  1. Create a useQueries call with duplicate queries (same queryKey / queryKeyHash) in an unsorted order.
  2. Inspect the result of useQueries to see they don't match with the input array

Another way to reproduce is through the unit test written in this pull request #8351

Expected behavior

We expect the input (Array) to #findMatchingObservers to match the output (Array)

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

This affects all operating systems/browsers

Tanstack Query adapter

None

TanStack Query version

5.59.6

TypeScript version

No response

Additional context

No response

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