How to refetch queries when the data are comming from the loader
I've a route where I load some fetch some data from react-query, however if I then do a mutaion and refetch that query it doesn't re-render the data in this component, I expect that the loader is only run on mount? How should I go about this?
3 Replies
graceful-blue•16mo ago
You can invalidate the router, but I wouldn't combine loaderData when you also use react query
Check out the examples - they only use the loaders to fill the cache, then always useSuspenseQuery in component
unwilling-turquoise•16mo ago
Additionally, I would put the jwt bit into
beforeLoad
(probably in the /_authenticated
layout route) so you can throw a redirect if the user is not authenticated. You can then return the token in beforeLoad
and it will be part of the context in the child route loader
methods.
@TkDodo 🔮 One question which I have which is related to this, what is the difference between
my initial guess is that parent routes could render while data is loading in prefetchQuery + useSuspenseQuery
, but I just want to make sure my understanding is right 🙂graceful-blue•16mo ago
There is no subscription to the query without useQuery / useSuspenseQuery
So no refetches and query can be garbage collected because it's seen as unused