How to set the setQueryData cacheTime to infinite?
I want to save some sync data in my cache for as long as possible. Right now it is garbage collected as soon as you refresh the page.
9 Replies
like-gold•4y ago
Hi, RQ is by default an in-memory cache. As such, on page reload, it is reset. To achieve what you want, you should look at persistQueryClient => https://tanstack.com/query/v4/docs/plugins/persistQueryClient. This would allow you to dehydrate the cache in localStorage for ex so that it can be re-hydrated and restored on page load.
adverse-sapphireOP•4y ago
Hi, thanks a lot, I will look at it! So RQ by default will reset ALL cached data on reload?
like-gold•4y ago
Yup
like-gold•4y ago
You could have a look here for an old codesandbox using RQ v3 and local storage persistor => https://codesandbox.io/s/react-query-some-queries-to-localstorage-dkpu8?file=/src/storage.js
adverse-sapphireOP•4y ago
Thanks! Its kinda funny since one of the main reasons I approached RQ is when I asked myself what would I do when user refreshes, and I thought this can be a GSM that can ALSO serve as cache logic haha...
like-gold•4y ago
Well the good news is that you can do both, with a small amount of code.
like-gold•4y ago
If you are new to RQ, I encourage you to read https://tkdodo.eu/blog/practical-react-query and the related posts. Really helpful to encompass what RQ can do
Practical React Query
Let me share with you the experiences I have made lately with React Query. Fetching data in React has never been this delightful...
adverse-sapphireOP•4y ago
Yea I was going for the approach of lets code until problems occur and then solve them hahaha
but reading is important, thanks I will look into everything youve sent!
rival-black•4y ago
There are many reasons why we can't persist something out of the box:
- it might be security relevant information (user-data) that we cannot write to a persistent store (localstorage) in the browser
- it won't work on react-native, or other non-browser environments
the persister plugins make this super easy if you want it, but you have to opt-in