Skip to content
Source
Sign inSign up
TanStack/query

useSuspenseQuery made extra request with old value #6486

Send to my agent
ClosedArcherDs(GitHub profile, opens in a new tab) opened this issue 2 comments

Describe the bug

When combine useSuspenseQuery with startTransition or useDeferredValue , will trigger extra request with the old value

I made some test and think the problem is introduced in 5.8.6

Your minimal, reproducible example

https://stackblitz.com/edit/stackblitz-starters-fhjkbj?file=src%2Findex.js

Steps to reproduce

click the incre button and look at the console

Expected behavior

not trigger extra request with the old value

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

all

Tanstack Query adapter

None

TanStack Query version

v5.8.6

TypeScript version

No response

Additional context

No response

  1. oh wow, this sucks. in 5.8.6, we added a fix to update the observer because suspense doesn't re-mount, it just visually hides the component:

    https://github.com/TanStack/query/blob/320cf9bed2ba9b0167aaf6f622665df3859ceed9/packages/react-query/src/useBaseQuery.ts#L94-L97

    It seems that this is the wrong thing if a transition is used, because the component re-renders one more time with the old data, so updating the observer is wrong.

    Not really sure how to fix this though ....

  2. TkDodo added labelsbug

  3. TkDodo added labelssuspense

  4. TkDodo added labelspackage: react-query

  5. I think I have another fix for the problem that we fixed in 5.8.6, but I wanted to have a failing test case first and I can't seem to reproduce this in our tests. I replicated your sandbox reproduction in a test, but it always works ... Can you maybe have a look at this test - am I doing something different than your sandbox?

    it('should not request old data inside transitions', async () => {
      const key = queryKey()
      let queryFnCount = 0
    
      function App() {
        const [count, setCount] = React.useState(0)
    
        return (
          <div>
            <button
              onClick={() => React.startTransition(() => setCount(count + 1))}
            >
              inc
            </button>
            <React.Suspense fallback={'Loading...'}>
              <Page count={count} />
            </React.Suspense>
          </div>
        )
      }
    
      function Page({ count }: { count: number }) {
        const { data } = useSuspenseQuery({
          queryKey: [key, count],
          queryFn: async () => {
            queryFnCount++
            await sleep(10)
            return 'data' + count
          },
        })
    
        return (
          <div>
            <div>{String(data)}</div>
          </div>
        )
      }
    
      const rendered = renderWithClient(
        queryClient,
    
        <App />,
      )
    
      await waitFor(() => rendered.getByText('Loading...'))
    
      await waitFor(() => rendered.getByText('data0'))
    
      fireEvent.click(rendered.getByText('inc'))
    
      await waitFor(() => rendered.getByText('data1'))
    
      await sleep(20)
    
      expect(queryFnCount).toBe(2)
    })
  6. TkDodo closed this

Sign in to join this conversation.