Redux-persist: Manual Persist

Created on 15 Jun 2016  路  9Comments  路  Source: rt2zz/redux-persist

Is it possible to manually persist state rather than having state automatically persisted on each change?

Most helpful comment

@KarlPurk Maybe you can try solving this problem with hot reloading.

@rt2zz : A possible production use case for this feature is the following: I have set a rather long debounce for performance reasons and would like to be sure that the state is persisted when the user exits my app.

The ideal interface, I think, would be something like this:

const persistor = persistStore(store, { debounce: 5000 });

window.onbeforeunload = function(){
  persistor.persist();
};

All 9 comments

not currently, but I would be open to ideas for what this would look like

@KarlPurk can you elaborate on why you want to manual trigger persist? If performance is an issue you can set a debounce on the persist, or also blacklist expensive reducers.

For development purposes. Manually persisting state is useful when debugging issues where the application has to be in a particular state in order to reproduce the bug. You can setup the application in the desired state and manually persist the state to storage - you're then in a position where you can make code changes and refresh the application to return to the same state quickly, allowing for easier debugging.

@KarlPurk Maybe you can try solving this problem with hot reloading.

@rt2zz : A possible production use case for this feature is the following: I have set a rather long debounce for performance reasons and would like to be sure that the state is persisted when the user exits my app.

The ideal interface, I think, would be something like this:

const persistor = persistStore(store, { debounce: 5000 });

window.onbeforeunload = function(){
  persistor.persist();
};

@simonacca clever. The api is not exposed for this currently, but I am open to suggestions. persistor.persist() seems ambiguous (ie. it is _already_ persisting). Some ideas:
persistor.flush()
persistor.persistImmediate()

This method would probably need to return a promise as persistence is typically (depending on storage engine) not sync.

Agreed, .flush() or .persistImmediate() is a much better API.

I am trying to implement this in #217 , let me know what you think.

it would be cool if I could persist the state even though it has not changed

Another use case that we are currently having:

We have a native app which also has a react native app. So certain parts of the app are implemented with react native and are using redux-persist. Before shutting down the react native part of the app, we want to make sure all the redux state got persisted and we don't kill of the react native (in terms of Android) activity too early before the state could be persisted.

flush is now part of the api, persistor.flush() which returns a promise that resolves when the flush is complete.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

admbtlr picture admbtlr  路  3Comments

umairfarooq44 picture umairfarooq44  路  3Comments

fredp96 picture fredp96  路  4Comments

peteroid picture peteroid  路  4Comments

ucarion picture ucarion  路  4Comments