redux-persist-expire-state

redux-persist transform to expire/reset reducer state after defined period of time

Usage no npm install needed!

<script type="module">
  import reduxPersistExpireState from 'https://cdn.skypack.dev/redux-persist-expire-state';
</script>

README

redux-persist-expire-state

Installation

yarn add redux-persist-expire-state

or

npm install redux-persist-expire-state

Usage

import { persistReducer, persistStore } from 'redux-persist';
import expireReduxState from 'redux-persist-expire-state';

const config = {
  expireAfter: 10 * 60 * 60, // Expiration time of 10 hours, defaults to null
  expireKey: 'persistedAt', // key to be used in the state, defaults to persistedTimestamp
  expireState: { error: null, data: []} // state to be used after persisted state expires, defaults to {}
};

const persistedReducer = persistReducer({
  transforms: [expireReduxState('reducer', config)] // reducerKey for which expiry is being set, add as many reducers as required
}, rootReducer);

const store = createStore(persistedReducer);
const persistedStore = persistStore(store);

Examples

// Reset `user` to empty object if not updated for 30 mins
expireReducer('user', {
    expireSeconds: 1800
})