Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jmtoung opened this issue Nov 25, 2024 · 1 comment

Comments

@jmtoung
Copy link
Contributor

jmtoung commented Nov 25, 2024

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

@jmtoung
Copy link
Contributor Author

jmtoung commented Nov 25, 2024

I have a pull request to address the issue #8351

@TkDodo TkDodo closed this as completed Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants