-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RTK Query] Expose "abort" from useQuery hook #3218
Comments
Usually, you don't want to First, you don't gain a lot by the But you lose things: that cache entry will go into an Why not just let the data come in fully and have them in a cache entry in case you need it again within the next 60 seconds or so? |
There are all sorts of problems with export |
Hi @phryneas , We also kind of need this. |
@berci-i wouldn't it make more sense to keep track of the arguments you have sent with the last request, and only perform work in the |
Thank you for the suggestion @phryneas I guess we could use something like localStorage in order to at least avoid carrying a variable around and/or adding an extra key in the reducer just for this. However it would have been a little easier for this case if we could just abort the requests. Thanks again! |
I'd avoid any localStorage usage here (it doesn't have any benefit over a variable, and you should use neither of those, and keep your reducers pure). Can't you just track the "latest" request when you see pending actions coming in? All of those requests do have a request id. I really don't want to expose |
@phryneas I understand the exposing part. I am not 100% sure what you mean with
Many thanks! 🙏🏽 |
Yup, something like that. |
I got an actual use case that the abort is needed. My team comes across a case that a refetch is called after a mutation is fired. The thing is that if there is already a pending query, the |
@WillisShek could this also be solved with #3116? |
I notice some difference in terms of query cancellation between react-query and RTK Q. In react query whenever a query consumes abortsignal, the query is automatically aborted on unmount or a new request is started while the prev one is running. RTK Q doesnt seem to have this built in instead we ll have to use Lazy query and plus a useeffect to handle this :/ |
@AsuraKev Yes, we don't do that by default because we think in most cases it's not a good idea to do this. |
Hmm sometimes user clicks fast and if we don’t cancel prev requests the requests could arrive out of order. Also raising abort signal means our API could terminate server processing as well which means we are not wasting resources on the server side. So I ll take it that if we wanna automatic cancel running query, the only way to achieve this is via lazy query? thanks 😀 |
@AsuraKev : even if the requests arrive out of order, the component that's asking for the data will only see the cache entry that it's asking for. Same as if there's 100 cache entries for different arguments to |
I see, thanks for clarifying 😃 |
It would be great (to have the option) to abort requests when the response is no longer needed (i.e. when the params have changed, or there are no subscribers). In some cases, mine included, processing the request on the backend can be very expensive in terms of time and/or money and aborting the request would allow the backend to detect the connection having been closed, and to stop the expensive processing. EDIT - this is a slightly different request so I have opened a new issue |
There is cases when abort might be useful to use while using useQuery hook to stop chain of queryFn requests, polling or long running tasks.
Proposal
Expose
abort
method from useQuery hook same as we do with "refetch"Use case
Inside queryFn I have a polling which submit the task (call A - once) and then poll task result by task id (call B - multiple with internal). Basically I need to abort data polling when it's not needed any more, for example on component unmount.
Currently we have to use useLazyQuery to achieve this:
What we want to achieve:
Or ideally it would be great if abort would happen by default on unmount internally so we don't have to do this manually... or controlled by additional option prop like "abortOnUnmount: boolean, abortOnArgsChange: boolean"
The text was updated successfully, but these errors were encountered: