T
TanStack2y ago
wise-white

Accessing react query without hooks

Hey, i would like to implement a expo background fetch (with notifications when api data changes) inside my expo app, but i realized that i cannot use the normal query hooks (useQuery etc.) inside the background callback. What would be the right way to get and set data inside the query cache, when the hooks are unavailable. If there is any (other than just "hard" accessing the cache directly)
6 Replies
afraid-scarlet
afraid-scarlet2y ago
you can use the query-core, which is independent of react
afraid-scarlet
afraid-scarlet2y ago
for a better understanding, please read: https://tkdodo.eu/blog/inside-react-query
Inside React Query
Taking a look under the hood of React Query
wise-white
wise-whiteOP2y ago
Thanks for the reply, I will take a look Is the core documented somewhere, or do I need to dig around to find the solution ?
afraid-scarlet
afraid-scarlet2y ago
the api reference is documented
wise-white
wise-whiteOP2y ago
So, i just played with it a bit and i need help. I have a QueryClient and AsyncStoragePersister instance. How do dump the data from storage into queryClient and then after some queries (that will be fetchQuery if i got it right) dump the new data back into the storage. (I am sorry for maybe dumb questions, but i just spent about 1hr on it and didn't get any smarter)
afraid-scarlet
afraid-scarlet2y ago
How do dump the data from storage into queryClient
with persistQueryClientRestore: https://tanstack.com/query/v5/docs/react/plugins/persistQueryClient#persistqueryclientrestore
dump the new data back into the storage
with persistQueryClientSave: https://tanstack.com/query/v5/docs/react/plugins/persistQueryClient#persistqueryclientsave or, you create a subscription with persistQueryClientSubscribe, which will restore when called and then sync all data back to the storage as long as the subscription is active: https://tanstack.com/query/v5/docs/react/plugins/persistQueryClient#persistqueryclientsubscribe this is quite well documented I believe
persistQueryClient | TanStack Query Docs
This is set of utilities for interacting with "persisters" which save your queryClient for later use. Different persisters can be used to store your client and cache to many different storage layers. Build Persisters

Did you find this page helpful?