`useQueries` doesn’t call `combine` when `StoragePersister` deserializes data #9586
Send to my agentDescribe the bug#
Take some queries that are 1) allowed to be stale, 2) are combined into something:
const ids = [1, 2, 3];
const combinedQueries = useQueries({
queries: ids.map((id) => ({
queryKey: ["post", id],
queryFn: () => Promise.resolve(id),
staleTime: 30_000, // ← 1️⃣
})),
combine: i => i, // ← 2️⃣
});
and 3) are persisted in localStorage:
const persister = createAsyncStoragePersister({ storage: window.localStorage });
const queryClient = new QueryClient();
<PersistQueryClientProvider
client={queryClient}
persistOptions={{ persister }}
>
In this setup, when the persister finishes deserializing the queries, and the queries aren’t stale yet, useQueries will not call combine (unless combine’s identity changes). As a result, combinedQueries will remain in isPending: true state until staleTime expires.
This only reproduces with a custom combine function, even if it simply returns the same value.
Your minimal, reproducible example#
https://codesandbox.io/p/sandbox/wqrzzm
Steps to reproduce#
To repro the issue:
- Open the codesandbox
- Confirm the preview shows “Pending: false, false, false. Data: 1, 2, 3”
- Reload the preview a few times
- Confirm the preview shows “Pending: true, true, true. Data: , ,” every time
The issue disappears if you pass a different `combine` on every render
- Unwrap
combineon line 27 fromuseCallback - Reload the preview a few times
- Confirm the preview shows “Pending: false, false, false. Data: 1, 2, 3” every time
The issue disappears if you don’t pass `combine` at all
- Remove
combineon line 38 - Reload the preview a few times
- Confirm the preview shows “Pending: false, false, false. Data: 1, 2, 3” every time
Expected behavior#
As a user, I expect to see “Pending: false, false, false. Data: 1, 2, 3” on every reload. However, I actually see “Pending: true, true, true. Data: , ,” starting with the second load.
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
- macOS 15.6
- Chrome 139
Tanstack Query adapter#
None
TanStack Query version#
v5.85.5
TypeScript version#
No response
Additional context#
No response
Still, I believe this should either be fixed in the library itself or clearly documented
I believe the latter is barely an option – due to the React Compiler. With RC, code that wasn’t memoized by the developer could still be memoized by the compiler (example – see how
composeis moved outside of the component to give it a stable reference?). Not fixing this would require putting"use no memo"into every component.Still, I believe this should either be fixed in the library itself or clearly documented
I believe the latter is barely an option – due to the React Compiler. With RC, code that wasn’t memoized by the developer could still be memoized by the compiler (example – see how
composeis moved outside of the component to give it a stable reference?). Not fixing this would require putting"use no memo"into every component.I agree with you. In React v19, React will decide whether to use memoization, not us. In other words, the default value for memoization will be true instead of false, so this issue should be resolved. So I explored the issue on my own and submitted a PR.
TkDodo added labelsbug
TkDodo added labelspackage: react-query
TkDodo closed this
Hello @iamakulov I just tested the bug-fixed version v5.85.9 on your codebase and confirmed the issue is resolved.
Sign in to join this conversation.
When searching the reproduction code for this issue, I noticed something peculiar: the key factor in reproducing it is that the combine function is wrapped with useCallback. If you remove useCallback from combine, everything works as expected.
If the documentation explicitly stated, “Do not use memoization (e.g., useCallback) with combine when using it together with persistence”, then this might not be considered a bug. However, the official documentation actually suggests the opposite.("This means that an inlined combine function, as shown above, will run on every render. To avoid this, you can wrap the combine function in useCallback, or extract it to a stable function reference if it doesn't have any dependencies.") In the useQueries reference , it even recommends wrapping combine in useCallback. That’s why I also believe this should be addressed—either by fixing the behavior or by clarifying the documentation.
After digging deeper, I found that the root cause lies in the #combineResult method of QueriesObserver. The conditions for executing the combine function look like this
On the first render after restoring from localStorage
!this.#combinedResultistrue(since it’s a new instance)combineis executed,inputpassed in hasisPending: trueanddata: undefined.Meanwhile, the cache itself actually holds
{ status: 'success', data: {…} }.Tracing further, the issue comes from how
QueryObserver.getOptimisticResultworks. when_optimisticResultsis set to'isRestoring', it fails to read the cached data and instead produces a pending result.Because a
useCallback-wrappedcombinehas a stable reference, that initial incorrect result persists. In contrast, an inlinecombinefunction is recreated on every render, so it re-runs and picks up the fresh data.Removing
useCallbackfromcombinemakes it behave correctly for now. Still, I believe this should either be fixed in the library itself or clearly documented, since the current recommendation in the docs contradicts the actual behavior.