In my React App, I want the user's cart and data to be cleared after a certain time.
I tried using a setTimeout() function that runs persistor.purge() after 1 hour, but this method doesn't work when you close the browser tab. So if the user, opens again the website after days, the store data is remained there.
Second method, I tried (redux-persist-expire) package, but nothing changes in the website. My code, index.js file,
import { persistStore, persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage";
import { createStore, compose, applyMiddleware } from "redux";
import thunk from "redux-thunk";
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const expireReducer = require("redux-persist-expire");
const persistConfig = {
key: "root",
storage: storage,
transforms: [expireReducer(rootReducer, { expireSeconds: 10 })],
};
const pReducer = persistReducer(persistConfig, rootReducer);
const store = createStore(pReducer, composeEnhancers(applyMiddleware(thunk)));
export const persistor = persistStore(store);
export default store;
Is there something needed to be fixed in the code? Or is there a better way to approach it.