Add ability to delete, or choose not to rehydrate the store in onRehydrateStorage
See original GitHub issueThis might have overlap with the issue #267, however this isn’t fully explained in that.
I notice there isn’t a great way to handle conditional persisted state rehydration.
In the onRehydrateStorage it would be nice to be able to either return state or throw an Error / return directly to not rehydrate the storage.
A little example; here I want to check if a cookie is still present, a JWT that expires after a week, and then choose not to rehydrate, the only option I found is below, to delete the localStorage key.
onRehydrateStorage: () => {
if (!hasJWT()) {
console.log("No JWT Present, Delete State");
localStorage.removeItem("web3-store");
}
},
An ideal API would be,
onRehydrateStorage: (state) => {
if (hasJWT()) {
return state
}
return false
}
Let me know if I’m missing something!
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:7 (2 by maintainers)
Top Results From Across the Web
zustand - Bountysource
Add ability to delete, or choose not to rehydrate the store in ... In the onRehydrateStorage it would be nice to be able...
Read more >React Redux Persist is rehydrating, but no stored data is ...
I'm testing with a store that doesn't have any saga in the middle, when I trigger an action, the data is updated, I...
Read more >How to Save State to LocalStorage & Persist on Refresh with ...
Step 2: Storing React state in localStorage. Now that we're successfully hiding our banner with state, we want to make sure when someone ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@mirshko @barbogast @AnatoleLucet
How I managed to make
zustand persistaccept asynchronous storage.First of all, I want to acknowledge the simplicity with which this library was created, otherwise I wouldn’t have the confidence to look into its source code and try a workaround. Big up team! 🙌🏼 My hopes are that this is not just a hack, but serves as an inspiration to others or maybe even a good fix for this amazing library.
Problem
As I said earlier, my issue was that
zustand’spersistmiddleware was overwriting the storage on hydrate, because there was no “initial” way of making it to or check for a condition before overwriting the storage or wait for a trigger to start hydration, before getting the storage - in case one has an asynchronous storage ( the edge case of an encrypted storage which is available after the user successfully enters the PIN / Password / biometrics ( most cases happen in React Native).Solution
onRehydrateStoragemethod to know when my hydration ends, I configured my persist store with empty options in order to block any storage overwrite or to solve any type error that comes if my getStorage is an async function.api.persist.setOptionsoverwriting in the process the emptygetStorageandonRehydrateStoragewith the methods that I need, and then I trigger a rehydration withapi.persist.rehydrate().Caveats
In order for this workaround to be successful, I had to take the unorthodox way and patch the library. Initial storage hydration is stopped, but at the same time makes any persist api methods unavailable as these are set later right before hydration, after the guard case where the middleware checks if the storage is set
In the patch, I’m moving the storage guard case after the
api.persistproperty is set and before hydrate is called. Of course, this throwsTypeError: Cannot read property 'setItem' of undefinedbecause now persist will continue to callsetItemon initialisation and for this I had to add an additional guard case in my patch to check if the storage exists there.Can anyone help what should be implemented for this issue?