Invalidating query/route after mutation
Hello! I am using TS router with TS query in a Vite SPA application.
I have a page that lists all events like so:
And then a page that allows me to create a new event:
The problem is that it is not invalidating after a new event is created! When I create a new event the list remains unchanged. What am I doing wrong?
8 Replies
like-gold•5mo ago
what exactly does not invalidate?
you should probably use a query hook instead of useLoaderData
like useSuspenseQuery
because useLoaderData will not be updated when the query refetches in the background
wise-whiteOP•5mo ago
It does not show the updated events list when I go back to the events page. Hummm so I should ensureQueryData on the loader, and then access it on the route using useSuspenseQuery?
like-gold•5mo ago
yes
this is how we do it in all our query based examples
wise-whiteOP•5mo ago
Ok I'm using that and now it works! But I see a problem: now whenever I go to the events page (e.g.: going back and forth from another page), I see a network request to the endpoint set in eventsQueryOptions request. How can I avoid that, and only load after a set period (or after invalidation)?
like-gold•5mo ago
stale time on query ?
wise-whiteOP•5mo ago
Yeah was missing that!
You've been great help Manuel! One last thing: when I create/update an event and then come back to the events page, there is a small time between the old list showing and the updated list showing. It's not a great deal, but is there a way to avoid this state change being visible to the user?
like-gold•5mo ago
usually you want this kind of stale-while-revalidate (SWR) behavior as it allows faster navigations
at the expense of briefly showing outdated data
one way could be to remove the query from the cache instead of just invalidating it
https://tanstack.com/query/latest/docs/reference/QueryClient/#queryclientremovequeries
cc @TkDodo 🔮
is there a better way?
eastern-cyan•5mo ago
await queryClient.fetchQuery
in the route loader