`combine` stable reference is not called with correct parameters #8781
Send to my agentDescribe the bug#
export default function Page() {
const [n, setN] = useState(0);
const queries = useQueries({
queries: [...Array(n).keys()].map((i) => ({
queryKey: [i],
queryFn: () => i,
})),
combine,
});
console.log(n, queries.length);
return <button onClick={() => setN(n + 1)}>Increase</button>;
}
const combine = (results: any) => results;
Clicking a few times the "Increase" button would give those logs:
0 0
1 0
1 1
2 1
2 2
3 2
...
Which is not the correct behavior.
When I'm not using a stable reference, i.e. directly passing combine:
export default function Page() {
const [n, setN] = useState(0);
const queries = useQueries({
queries: [...Array(n).keys()].map((i) => ({
queryKey: [i],
queryFn: () => i,
})),
combine: (results) => results,
});
console.log(n, queries.length);
return <button onClick={() => setN(n + 1)}>Increase</button>;
}
I get those logs:
0 0
1 1
1 1
2 2
2 2
3 3
...
Which is the correct behavior.
Your minimal, reproducible example#
https://codesandbox.io/p/devbox/lucid-danny-rjy97g
Steps to reproduce#
Open the repro. Click on "Increase". Look at the logs.
Expected behavior#
0 0
1 0
1 1
2 1
2 2
3 2
...
How often does this bug happen?#
None
Screenshots or Videos#
No response
Platform#
Linux Chrome
Tanstack Query adapter#
react-query
TanStack Query version#
5.59.20
TypeScript version#
No response
Additional context#
No response
Hey @joseph0926 , thank you for your answer!
First, I believe that the behavior when passing a stable reference and when not passing a stable reference should be the same. That alone is already an issue to me.
Second, the result of useQuery should reflect the queries passed to useQuery. If I change the queries, it should reflect this in the results. So basically step 6 is not correct to me.
Similarly, right now, when I change the key of a query, it immediately sets back
isPendingto true anddatato undefined. It doesn't return me the data of the previous query for one render cycle, and then update in the next one. And I think this is the correct behavior.@lcswillems After hearing your explanation, I think you're right! React Query API shouldn't depend on referential stability for the correctness of results
First, I believe that the behavior when passing a stable reference and when not passing a stable reference should be the same. That alone is already an issue to me.
This is merely a performance optimization. When the reference is stable, you can’t close over values, so there’s no need to re-run combine on a render if
datahasn’t changed - we can re-use the cached value.For inline functions, that’s not the case. It’s really the same as we do with
selectonuseQuery: https://tanstack.com/query/v5/docs/framework/react/guides/render-optimizations#memoizationHowever, it can of course be that we got something wrong in this caching. I don’t have the time right now to look into it. A PR with a failing test case would already be very helpful.
TkDodo added labelspackage: query-core
TkDodo added labelshelp wanted
Can you assign this to me please? I might be able to fix this. Thanks!
TkDodo assignedgopnik5
@gopnik5 had taken ownership of the fix, and his proposed direction was similar to mine. However, since there has been no response for several months and no tests seem to be included, I am opening an additional PR to help resolve the issue.
If there is a rule that prevents someone else from submitting a PR once an issue has already been assigned, I will be happy to close this one.
TkDodo unassignedgopnik5
I've worked on the issue in question. Please take a look. @TkDodo
TkDodo closed this
Sign in to join this conversation.
Isn't this a natural behavior caused by the function reference value not changing?
https://github.com/TanStack/query/blob/79608f306991cfea026107714b5b5368e29172d2/packages/query-core/src/queriesObserver.ts#L209
Looking at the “if” statement in the link, it looks like the intended behavior.
Wouldn't it probably go something like this?
(I used a translator)