-
Notifications
You must be signed in to change notification settings - Fork 867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Manual rehydrate #952
Comments
I'm not sure how to to do that, but I'm curious about your use case... why do you want to stall rehydration until there is internet? Are you using a custom storage that requires internet? My app just uses localStorage, so I don't care if there is internet during rehydration. Also, note the v4->v5 updates for autoRehydrate: https://github.com/rt2zz/redux-persist/blob/208c6b112ead87b3701dfacaae2cdbe78377775a/docs/MigrationGuide-v5.md#migration-from-v4-to-v5 Maybe something in there will help you figure out what to do here. |
Because in my application(chatting app) i store messages locally, but what if messages was changed when i was offline ? (like deleted or changed text). How do i know that i need to delete my local message or change it to be the same as server version? |
@bohdan145 It might not be the best way, but I believe you can just intercept the 'persist/REHYDRATE' action for your desired store(s). In your store's reducer you can either return the hydrated payload or your original state. Inside your desired reducer you would add something like this:
That would at least allow you to prevent hydration based on your internet connection. I'm sure there is a way to re-trigger the hydration logic once the internet is restored although I am not sure how. Maybe you can dispatch the REHYDRATE action or re-initialize the persistor to accomplish this. |
Hi I am interested in this as well. I need to stall the rehydration until my storage is initialized, which can only be done after the user entered a password which will be used for file level decryption in the implementation of the storage. |
@aguynamedben Do you have any ideas whether this is possible or not, or who might now? |
@leoek I think you can accomplish what you need by ignoring the initial hydration and then manually re-hydrating using getStoredState. Once you have the state you can dispatch the necessary actions to update your app state as needed. If you use my example above you could dispatch 'persist/REHYDRATE' with shouldHydrate set to true and include the data from getStoredState in the dispatched action. If that doesn't work for whatever reason, another idea would be to delay the persistor initialization and adding the persistedReducer to your store until your storage is initialized. That seems like it should work in theory, but might get messy. |
@cvanem Thank you for the suggestion. This seems like a viable option. In theory I only need to encrypt parts of the state i am persisting and I should be able to use multiple persistors. One configured like this:
and one for each encrypted reducer which is initialized later, configured like this:
Are there any cave hats with the usage of multiple persistors, you know of? |
To me it seems "obvious" that a manual rehydration would reset the store just the same as restarting the app, white/blacklisting taken into account. Wouldn't it be a common use cast, "just discard all the state not whitelisted", for example? |
@mauriciopasquier rehydration does not reset the state. I believe the default reducer for rehydration does a shallow merge. If you need to merge within a deeper level you can use your own reducer. (Manual means delayed until a manual callback is triggered. You can check the implementation in #986 This is only a very little change. We are using it in production for quite a while now, without any issues.) |
@leoek thanks for your reply, I guess I'll implement "restarting" manually then, but I wanted to KISS and DRY with redux-persist white/blacklisting configuration. |
@mauriciopasquier Your requirement seems unrelated to this issue. I think it is best if you open a new issue and pull request for that. |
@cvanem Can you elaborate a bit on what |
@bitflower |
Thanks @leoek I swear I had previously tried to import it but didn't find it. Got it now ;-) @cvanem says "I think you can accomplish what you need by ignoring the initial hydration and then manually re-hydrating using getStoredState." - I wonder how this can be achieved on a global level. Not on a per-reducer basis as also described in this thread. |
This was about doing the rehydration manually at a custom point in time (instead of doing it right after the application startup). However to achieve this you don't need to use getStoredState manually, as it was integrated into redux-persist with #986 |
@leoek I've played around with |
btw I've decided to add the reducer and persistor only after the user is logged in. I need seperate storages per user. This way I hope to be able to customize the storage after login and then "add" the persistor. |
Setting
This is exactly what it was intended for. |
Aha, so procedure would look something like: 1: Setup store incl. persistence, manualPersist: true I wonder how to achieve 3: when the persist config was already defined in 1: Thanks again for some more breadcrumbs ;-) |
Nvmd, found it out. I can set the config again in I might PR a fix for the
Do you use typescript @leoek ? Thanks again for your input and have a nice Sunday :-) |
I have a similar issue. I need to hydrate the store only once the user has logged in, so that I can create a persistent store only by using the user id in react native (in case the user uses different account to login in the app) |
How can i stop automatic re-hydration and launch it manually only if there is no internet connection ?
The text was updated successfully, but these errors were encountered: