Replies: 2 comments 3 replies
-
Beta Was this translation helpful? Give feedback.
-
query invalidation doesn't refetch per se, and it also never removes any data. It refetches queries that are active and stale, and inactive queries get marked as stale so that they refetch when you next mount them. when a query that already had successful data once gets an error, it will have an you can also work with |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey! I am running into undesired UI states when trying to invalidate queries. The issue appears to be particularly with the infinite queries.
Scenario:
A user can belong to a team. Team contains records. An owner can delete the team, which also deletes all records.
Desired outcome:
If I visit the team records page after deleting the team, I should see an error because the team was deleted.
Actual outcome:
The team records page shows cached data.
How do I cache data?
All team related queries start with
team-*
. After deleting a team, I callqueryClient.invalidateQueries('team')
. This should invalidate all team queries and it appears to do so.How do I fetch data?
I fetch records using
useInfiniteQuery()
, nothing fancy.Problem:
When I view the team records page after deleting the team, the API request fails with 404 as the team no longer exists. However, I can see the previous infinite query data is still there. It appears that due to the error response, the cache does not get cleared/overridden. In my case, returning
undefined
would work. Strangely, theisError
response isfalse
, too. If I refresh the page,isError
becomestrue
as expected. I am unable to test the cache state as there would be no previous data at that point.Am I doing something wrong or is this a bug with
react-query
?Update:
I can achieve the expected behaviour if I invalidate the specific query for team records. This is not maintainable though as I'd have to remember to clear every team feature (very easy to forget to do so).
Update 2:
The solution above still does not clear the data, but it correctly sets
isError
totrue
which is enough in my case. Not sure why it's nottrue
when using fuzzy match.Beta Was this translation helpful? Give feedback.
All reactions