-
Good afternoon, There might be a need to store only certain keys in local storage rather than persisting our entire state app there. I wonder if there's anything like in react-query persistor. Or for now it only has capability to store all keys in local storage. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 9 replies
-
the persistors are build upon
per default, this is implemented as: |
Beta Was this translation helpful? Give feedback.
-
I am trying to achieve this in react native, but cannot get it to work. This is my basic setup:
Obviously I should replace the content of shouldDehydrateQuery with some actual logic in order to conditionally persist only the keys that I actually want to persist. In the code above, I left "return true" and the commented out "return false" just to show how I went about trying to implement this. Both when I use "return true" and "return false" and then log the content of my AsyncStorage, the storage includes the whole cache (in my case that means the keys 'users', 'characters' and 'locations'), whereas I'd expect it to not include any of the query keys in at least one of the cases. I cleared the content of the storage in between trials. |
Beta Was this translation helpful? Give feedback.
-
One caveat to mention here is if you have To solve this, I wrote a custom serializer that overwrites the cache |
Beta Was this translation helpful? Give feedback.
the persistors are build upon
hydration
, and it acceptsdehydrateOptions
, so:dehydrateOptions
accept ashouldDehydrateQuery: (query: Query) => boolean
function, where you can decide which query should be persisted, and which should not.per default, this is implemented as:
query.state.status === 'success'
to avoid persisting loading or erroneous queries.