Giter VIP home page Giter VIP logo

Comments (4)

AlgirdasVZ avatar AlgirdasVZ commented on May 18, 2024 1

Aaah, apologies, seems to be my mistake, related with how query client was being imported. Query canceling seems to be working just as described. Thanks for your help! I believe this issue can be closed

from query.

TkDodo avatar TkDodo commented on May 18, 2024

I think you're right, that's why the flag is called cancelRefetch and not cancelFetch - it can only cancel a refetch, and a refetch is a fetch where we already have data.

Not sure why we have that implementation / limitation. I would argue that cancelFetch is more appropriate - why would we only do this for refetches. But not sure if we can fix this in a non-breaking way? If we change the implementation and remove the this.state.dataUpdatedAt check - it would change the behaviour and the cancelRefetch name would be confusing.

I tried that, but we do have a test that covers this scenario:

it('should not cancel an ongoing fetch when refetch is called (cancelRefetch=true) if we do not have data yet', async () => {
const key = queryKey()
let fetchCount = 0
function Page() {
const { refetch } = useQuery({
queryKey: key,
queryFn: async () => {
fetchCount++
await sleep(10)
return 'data'
},
enabled: false,
})
React.useEffect(() => {
setActTimeout(() => {
refetch()
}, 5)
setActTimeout(() => {
refetch()
}, 5)
}, [refetch])
return null
}
renderWithClient(queryClient, <Page />)
await sleep(20)
// first refetch will not get cancelled, second one gets skipped
expect(fetchCount).toBe(1)
})

So, I really don't think we can change this right now. next major maybe? I always prefer less code and less surprising behaviour, but I also don't want to change this now without renaming the flag.


For your cases - what you can do is call queryClient.cancelQueries({ queryKey }) to manually cancel it instead of letting refetch take care of it. That should work.

from query.

AlgirdasVZ avatar AlgirdasVZ commented on May 18, 2024

Hi @TkDodo, thanks for your answer!

I agree that the current flag name (cancelRefetch) works exactly as the name implies - it only cancels refetches. cancelFetch would make more sense, since the current behavior of cancelRefetch creates that blind spot, which might not be obvious for library users. Both flags could potentially coexist, but it would only make dx more confusing IMO, so I also agree with you that it would only make sense introducing it as a breaking change somewhere down the road (if such decision would be made).

P.S queryClient.cancelQueries was what I tried initially - this does not seem to cancel the initial fetch either, only the following ones. This also matches with the cancelQueries description, where canceling refetches is mentioned as a use case:

This is most useful when performing optimistic updates since you will likely need to cancel any outgoing query refetches so they don't clobber your optimistic update when they resolve.

from query.

TkDodo avatar TkDodo commented on May 18, 2024

this does not seem to cancel the initial fetch either

Hmm I looked at the code and queryClient.cancelQueries should also cancel initial fetches.

I wrote a quick test for that to confirm, and it passes:

test('should also cancel initial queries', async () => {
  const key = queryKey()
  void queryClient.fetchQuery({
    queryKey: key,
    queryFn: async () => {
      await sleep(10)
      return 'data'
    },
  })
  await queryClient.cancelQueries({ queryKey: key })

  const state = queryClient.getQueryState(key)

  expect(state).toMatchObject({
    data: undefined,
    status: 'pending',
  })
})

from query.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.