v5.85.2 introduced a breaking change while using StrictMode #9579
Send to my agentDescribe the bug#
v5.85.2 introduced a breaking change while using StrictMode impacting state of isLoading and isFetching
On initial load state of isLoading doesn't reflect described API behaviour, API states that isLoading reflects the state of the first fetch, not subsequent fetches. However with StrictMode enabled isLoading reverts to false before the first request finishes
Example: https://stackblitz.com/edit/tanstack-query-2c4dbawu?file=src%2Findex.tsx
Example above updates DOM as request state changes: req1: isLoading:false, state: success, fetching: false data: request 1, time=1755607826585. time= is added when request is finished
Your minimal, reproducible example#
https://stackblitz.com/edit/tanstack-query-2c4dbawu?file=src%2Findex.tsx
Steps to reproduce#
- On initial load observe that
isLoading: true,state: pendingandfetching: false - Observe
isLoadingquickly changes tofalsebutstateremainspending.
Before v5.85.2 isLoading would remain true until first request completes. If StrictMode is removed isLoading behaves as expected
Downgrade to 5.85.0 npm install @tanstack/react-query-devtools@5.85.0 @tanstack/react-query@5.85.0 and run the example again npm run dev and observe isLoading behaves as expected.
Expected behavior#
At first load isLoading and isFetching should reflect the status of the first request.
How often does this bug happen?#
Every time
Screenshots or Videos#
https://github.com/user-attachments/assets/58464f7e-c7bf-4246-a0c3-3debf3e97882
Platform#
- OS: macOS
- Browser: Chrome
- Version: 5.85.2
Tanstack Query adapter#
None
TanStack Query version#
5.85.2
TypeScript version#
No response
Additional context#
No response
TkDodo changed the title
This issue does not appear to be solely related to
StrictMode, but rather occurs when usingsignalwithin thequeryFnunderStrictMode. In fact, if you remove thesignalfrom the provided code as shown below,queryFn: () => { return new Promise((resolve) => { setTimeout( () => { const now = Date.now(); console.log(`fetchCompleted index=${index}, time=${now}`); resolve(`request ${index}, time=${now}`); }, 4000, // { signal } ); }); },isLoadingwill correctly becometrue.The problem arises because when the
signaloption is present and the fetch starts, destructuring the signal setsabortSignalConsumed = truehttps://github.com/TanStack/query/blob/7c464e3ded7c4764170839b4aa7f763c7d0aa647/packages/query-core/src/query.ts#L428. Due toStrictMode, during cleanup the following condition is met insideremoveObserverif (this.#abortSignalConsumed) { this.#retryer.cancel({ revert: true }); }This triggers an asynchronous execution of
this.#retryer.cancel({ revert: true }). As a result, the sequence becomes: immediate re-mount → new observer added → second fetch begins → meanwhile, the asynchronouscatchlogic from the first fetch executes, which then hits the following branchelse if (error.revert) { this.setState({ ...this.#revertState, fetchStatus: 'idle' as const, }) }This causes the state to be overwritten with the initial values, leading to the observed issue.
The reason it did not occur in the previous version is that
onErrorexisted and handled the case, but now the logic is fully asynchronous and falls intocatch, which results in the problem you are seeing.Same here, seems like isFetching is not awaiting promise to be fulfilled. 5.85.1 - works as expected 5.85.2 - broken
we're experiencing the same issue - completely broke the
isLoadingbehavior for us.Same here
completely fucked up isLoading to an extent I can't use the current version
completely fucked up isLoading to an extent I can't use the current version
This software is provided to you for free. A little respect for the maintainers would be in place.
Otherwise I agree with you, something is clearly wrong.
Yep, workaround is to downgrade to 5.85.1 for the time being.
completely fucked up isLoading to an extent I can't use the current version
I would’ve fixed this sooner but I was enjoying my vacation 🌴 . I also don’t consider this a huge regression because you explicitly needed StrictMode + AbortSignal to trigger it, and even then, only in DEV mode of course.
Still, it’s a but and the fix is up:
TkDodo added labelsbug
TkDodo closed this
@TkDodo great to have you back 👋
@TkDodo Thank you for the fix! Will try as soon as possible. Sorry if my comment came off as rude, it wasn't intended.
Sign in to join this conversation.
Slight correction: It seems the behaviour first occurred in 5.85.2