GeezerVison is showing github.com — `useQueries` doesn’t call `combine` when `StoragePersister` deserializes data · Issue #9586 · TanStack/query
useQueries doesn’t call combine when StoragePersister deserializes data #9586
DescriptionDescribe 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
combine on line 27 from useCallback
- 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
combine on 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
Tanstack Query adapter
None
TanStack Query version
v5.85.5
TypeScript version
No response
Additional context
No response Activityjoseph0926 commented on Aug 25, 2025 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
if (
!this.#combinedResult ||
this.#result !== this.#lastResult ||
combine !== this.#lastCombine
) {
// run combine
this.#lastCombine = combine
this.#lastResult = this.#result
this.#combinedResult = replaceEqualDeep(
this.#combinedResult,
combine(input),
)
}
On the first render after restoring from localStorage
!this.#combinedResult is true (since it’s a new instance)
- so
combine is executed,
- but the
input passed in has isPending: true and data: undefined.
Meanwhile, the cache itself actually holds { status: 'success', data: {…} }.
Tracing further, the issue comes from how QueryObserver.getOptimisticResult works. when _optimisticResults is set to 'isRestoring', it fails to read the cached data and instead produces a pending result.
Because a useCallback-wrapped combine has a stable reference, that initial incorrect result persists. In contrast, an inline combine function is recreated on every render, so it re-runs and picks up the fresh data.
Removing useCallback from combine makes 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. iamakulov commented on Aug 25, 2025
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 compose is moved outside of the component to give it a stable reference?). Not fixing this would require putting "use no memo" into every component. joseph0926 commented on Aug 26, 2025
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 compose is 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.
#9592 joseph0926 commented on Sep 2, 2025 Hello @iamakulov
I just tested the bug-fixed version v5.85.9 on your codebase and confirmed the issue is resolved. Reacted by Dominik Dorfmeister 🔮
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|