React · Senior · Question 38 of 75
How do you properly support request cancellation in TanStack Query?
- ✓Correct answer: Pass the
signalfrom thequeryFnargument tofetch - 2Call
AbortController.abort()inonSuccess - 3Enable the global option
cancelOnUnmount: true
Explanation
queryFn: ({ signal }) => fetch(url, { signal }). The library cancels the request if the component unmounts before the response, the key changes, or cancelQueries is called. Without passing signal, only result handling is cancelled while the HTTP request itself continues.