gabceb / redux-persist-transform-expire

Add expiration to your persisted store
MIT License
64 stars 19 forks source link

Call method on expiration #10

Closed jdolle closed 6 years ago

jdolle commented 6 years ago

When the state expires, I want to fetch new state from my API. I'd prefer to not duplicate the time checking logic -- so I'd like to have an option to call a method on expiration, allowing me to make a request to my api then dispatch an action to set the redux state (and therefore persist the new state).

gabceb commented 6 years ago

You would only get this method to execute on rehydration of redux persist. Can you add the logic on a saga/thunk that happens after that action is fired?

jdolle commented 6 years ago

That makes sense and sounds like a good solution. Thanks!

If there is a better way to do this then please comment, but I am now doing:

const unsubscribe = persistor.subscribe(() => {
    const { bootstrapped } = persistor.getState();

    if (bootstrapped) {
      if (store.getState().exampleState.persistExpiresAt === undefined) {
        store.dispatch(fetchExampleStateAction());
      }
      unsubscribe();
    }
  });