query: useQuery().promise is not restored when staleTime is set
Describe the bug
I was trying out the new useQuery().promise implementation with the React.use() hook, and noticed two issues with it.
For the reproduction I have a very simple setup similar to the docs. There is an input that updates the q state onChange, and the following query:
useQuery({
queryKey: [q],
queryFn: () => getData(q),
staleTime: 60000,
});
There’s also a Data component wrapped in a Suspense, which take the promise of the query above as a prop and calls use on it. The query function takes the q, delays 1s and returns the same:
const getData = async (q) => {
await new Promise((resolve) => setTimeout(resolve, 1000));
return { q };
};
The first issue occurs when staleTime is set, and it still has fresh data. In this case it seems that the promise in not being restored, and incorrect data is shown. Here’s a screen recording:
https://github.com/user-attachments/assets/101dd91e-fdb4-433e-85a8-4e897172821f
When typing extra characters, it correctly updated the data. However, when I remove characters (in which case it already had fresh data cached) it doesn’t seem to restore the correct promise.
The devtools is showing the correct query as active, and the Data component is re-rendering. but from the console logs it seems to be receiving an incorrect promise.
<Search> requested query: "1234". received promise results: {q: '12345'}
<Search> requested query: "123". received promise results: {q: '12345'}
<Search> requested query: "12". received promise results: {q: '12345'}
<Search> requested query: "1". received promise results: {q: '12345'}
This issue does not occur when the queryFn is ran, e.g. when providing a query that is not cached or when staleTime: 0.
And the second one, the suspense is only triggering once, and not when the query changes. In the screen recording you can notice as I type “12345”, it no longer triggers the suspense (which has “suspending…” as fallback), but it provides a stale promise, until it’s done fetching the data for the new query.
I was expecting the suspense to trigger when the query key changes and it’s about to fetch data. I thought the current behavior (i.e. show stale data until the new render is done) would have to be achieved through useDeferredValue instead.
I am not sure if this is the intended behavior, so I am did not create a new issue for it, but please let me know if I should do that.
Your minimal, reproducible example
https://codesandbox.io/p/sandbox/awesome-williamson-k2269m
Steps to reproduce
On the first issue, simply type anything in the input and notice the data under it updating. Now remove characters and notice that incorrect data is shown.
On the second issue, notice the suspense triggering when reloading the page, but not when changing the query (in which case query.isFetching is indeed true).
Expected behavior
Already explained above.
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
- OS: Windows 11
- Browser: Chrome
- Version: 129.0
Tanstack Query adapter
react-query
TanStack Query version
5.59.0
TypeScript version
N/A
Additional context
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 15
Thanks for the update. I tried out the latest release and that is working great now!
I came across another case that suspends indefinitely. It happens consistently when the promise and the suspense are colocated, but the promise fails. I modified one of the existing test, it currently fails waiting for the error boundary. I have filed a new issue for better tracking: https://github.com/TanStack/query/issues/8219.
took a bit longer but I found it:
alright, shipped it so that you don’t have to use preview builds for that 😃 enjoy 🍻
@TkDodo You’re welcome! I’m glad to help out. I tested the new change, and it works much better when typing now.
The only potential regression that I noticed, is that it’s now suspending briefly when it has cached data. I suppose in this case a fulfilled promise should be provided instead of a pending one which resolves later (that was the case previously)?
Here’s a screen recording where you can see that as I remove characters from the input:
https://github.com/user-attachments/assets/599aa904-0d52-4fae-8ea6-997dbba765ad
Thanks for reporting 🙏
The first one with
staleTimeis definitely wrong, we need to look into this.As for the second one, that’s also an issue 😂 . When we go to a new Query with a key that has no data, we need to suspend again. You’re absolutely right that you would need deferred values, transitions or our
placeholderData: keepPreviousDatafeature to keep “old data” from a different key on the screen.@KATT FYI.