clear cache on error
Hi, currently when a query fails, it would contain
error and previously cached data .
This is undesired for my use-case, but I can't figure out a way to achieve that.
To be clear,
Expected: useQuery only returns error but no data when the request fails.
Reality: useQuery returns both error and previously data when the request fails.
Is there any suggested way to achieve this?8 Replies
xenial-black•4y ago
you can always decide to not render the data if you are in error state ...
fascinating-indigoOP•4y ago
yes I know, that's what I do now.
It just seems weird with all the conditionals, and the data doesn't even associate with that error.
xenial-black•4y ago
not sure I share / understand that concern. The error you're talking about seems to be a background error - that is, an error that occurs after you have once successfully fetched data. You can show this error if it's important, or, you can decided if that error can be ignored because you have stale data and can still show that. It's not more conditionals, it's just a different order:
this prefers stale data. flip the two ifs around to prefer errors over stale data 🤷♂️
fascinating-indigoOP•4y ago
the requirement I'm working on is searching for a user information, when error occurs it might be that the user has be deleted, in that case we want to clear all cache of that user information and show error that the user does not exist.
the content is not the only thing the component returns, so I used
data && ... and error && ..., with this way I can't prefer one before another, they both show, that's why I needed more conditionals.
I should probably abstract and componentize better to solve this.
Thanks for answering this weird question!🙇xenial-black•4y ago
when error occurs it might be that the user has be deleted, in that case we want to clear all cache of that user information and show error that the user does not exist.sounds like an onError callback that removes that data with queryClient.removeQueries
fascinating-indigoOP•4y ago
won't this cause the query to refetch?
I had used invalidateQueries with
refetchType: 'none', but then decided to keep it clean, since if request succeed it will replace the data anyway.xenial-black•4y ago
no,
remove only removes it an doesn't refetch (that what reset would do)fascinating-indigoOP•4y ago
That’s great to know. Thank you!