I need to store a value, asynchronously, offline. Can this be done? #1288
Replies: 1 comment 1 reply
-
Since react-query is promise-based, you could build a dependent query in user-land by making a useQuery that reads from the localForage and build something like this:
you would still need to hande the state where both queries are not-yet loaded, but I think this should work. |
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.
-
Here's the idea: I have a very large network request, but it is performed conditionally since the data doesn't change much. I do this by obtaining a unique identifier (a string) from the server that will be saved to local storage. My query looks as follows:
All is well and good here. So this allows me to really conveniently do an "offline" stale-while-refetch like behavior. However, in my other query for the actual validation schema, I want to, by default, use an offline version loaded asynchronously from some async storage. In this case, I am using
localForage
.So, I basically do the following in my main query:
Now here's the weird catch: the localForage version is actually asynchronous. I want to fetch and sync with the async storage when the query changes, and also default to initially using async storage. The "unresolved initial" value can just be null
Is this possible to do?
Beta Was this translation helpful? Give feedback.
All reactions