Comments (4)
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.
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:
query/packages/react-query/src/__tests__/useQuery.test.tsx
Lines 514 to 546 in 1f0088e
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.
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.
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)
- useQuery doesn't change data after query invalidation HOT 6
- Nextjs Streaming+Server Action Error: Server Functions cannot be called during initial render. HOT 1
- useQueries and large datasets is crashing browsers HOT 5
- Spreading an any type into UseQuery makes the data unknown HOT 2
- Doc site is down HOT 1
- No QueryClient set, use QueryClientProvider to set one HOT 2
- vue-query suspense always throws render errors HOT 1
- `result` parameter in `combine` function in `useQueries` initially empty array HOT 1
- useInfiniteQuery not being refetch on param change HOT 5
- solid-query modifies query data on refetch before suspense triggers HOT 1
- Cannot read property 'prototype' of undefined when declaring `new QueryClient()` on React Native HOT 9
- useInfiniteQuery `data.pageParams` is always unknown type HOT 6
- Upgrading 5.15.0 -> 5.17.0 causes severe lag when switching from specific route to another HOT 5
- When used in conjunction with the latest version of "[email protected]," there may be an error. HOT 2
- Angular Query: type narrowing not working on query and mutation result HOT 3
- handling error in QueryCache with suspense query together, error throwing is inconsistent. HOT 1
- Calling useQueries with an empty array leads to unlimited rerenders with the latest version of query HOT 2
- Using state inside `combine` function uses previous value HOT 2
- NPM: eslint-plugin-query v4.38 tagged as latest HOT 2
- Optional Suspense Support in React-Query Hooks
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from query.