Is it possible to wait for a few seconds before invalidates the cache in onSuccess?
I have tried this, but it does not work as I expect
7 Replies
foreign-sapphire•3mo ago
What do you expect?
What is happening?
equal-aquaOP•3mo ago
I'm trying to upload a file. When I upload success and immediately invalidate the cache. It seems like the upload process is not done on the server, so I can not get the latest content. I want to wait a few seconds before invalidating the cache
This works, but I want to keep the loading state while waiting
following-aqua•3mo ago
If you want to retain loading state, you need the mutationFn to still be running. The easiest way to do this is to simply put your sleep in your mutationFn, which will cause it to take longer
Personally, I think of the callbacks on useMutation less like
.then(...)
,
.catch(...)
,
.finally(...)
,
and more like
.addEventListener("success", ...)
,
.addEventListener("error", ...)
,
.addEventListener("settled", ...)
,
Alternatively, if you are submitting inside a form, my general advice to my team members is to always use mutateAsync
, because the form will handle the loading state rather than the mutation. For example:
This way the form will typically remain loading while you are doing the extra work. If you are not using a form, this can be ignoredequal-aquaOP•3mo ago
Great! It works as I expect. Thanks
afraid-scarlet•3mo ago
It seems like the upload process is not done on the serverthe server should likely not return unless it's done with the upload then?
equal-aquaOP•3mo ago
I'm not sure, but I think it still works properly, doesn't it? Because the process is not done, when I call api to get the content, it will return the current content
flat-fuchsia•3mo ago
Isn't it also possible that your original source of truth (useQuery) is revalidated due to expired cache/TTL or some other action, for example: window focus.