State Management · Senior · Question 28 of 75
How do you cancel an in-flight request when a query becomes unused in TanStack Query?
- ✓Correct answer: Pass the
signalfrom thequeryFncontext tofetch - 2Call
AbortController.abort()inonSuccess - 3Enable a global
cancelOnUnmountoption
Explanation
queryFn: ({ signal }) => fetch(url, { signal }). The library aborts when the component unmounts before the response, the key changes, or cancelQueries is called. Without signal only result handling is cancelled.