Skip to content
Source
Sign inSign up
TanStack/query

v5.85.2 introduced a breaking change while using StrictMode #9579

Send to my agent
Closedpvs-chainalysis(GitHub profile, opens in a new tab) opened this issue 11 comments

Describe 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

  1. On initial load observe that isLoading: true, state: pending and fetching: false
  2. Observe isLoading quickly changes to false but state remains pending.

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

  1. TkDodocommented

    Slight correction: It seems the behaviour first occurred in 5.85.2

  2. TkDodo changed the title

  3. This issue does not appear to be solely related to StrictMode, but rather occurs when using signal within the queryFn under StrictMode. In fact, if you remove the signal from 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 }
        );
      });
    },

    isLoading will correctly become true.

    The problem arises because when the signal option is present and the fetch starts, destructuring the signal sets abortSignalConsumed = true https://github.com/TanStack/query/blob/7c464e3ded7c4764170839b4aa7f763c7d0aa647/packages/query-core/src/query.ts#L428. Due to StrictMode, during cleanup the following condition is met inside removeObserver

    if (this.#abortSignalConsumed) {
      this.#retryer.cancel({ revert: true });
    }

    https://github.com/TanStack/query/blob/7c464e3ded7c4764170839b4aa7f763c7d0aa647/packages/query-core/src/query.ts#L351-L352

    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 asynchronous catch logic from the first fetch executes, which then hits the following branch

    else if (error.revert) {
      this.setState({
        ...this.#revertState,
        fetchStatus: 'idle' as const,
      })
    }

    https://github.com/TanStack/query/blob/7c464e3ded7c4764170839b4aa7f763c7d0aa647/packages/query-core/src/query.ts#L555-L567

    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 onError existed and handled the case, but now the logic is fully asynchronous and falls into catch, which results in the problem you are seeing.

  4. Same here, seems like isFetching is not awaiting promise to be fulfilled. 5.85.1 - works as expected 5.85.2 - broken

  5. we're experiencing the same issue - completely broke the isLoading behavior for us.

  6. completely fucked up isLoading to an extent I can't use the current version

  7. 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.

  8. Yep, workaround is to downgrade to 5.85.1 for the time being.

  9. TkDodocommented

    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:

  10. TkDodo added labelsbug

  11. TkDodo closed this

  12. @TkDodo great to have you back 👋

  13. @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.