小编Mis*_*Tea的帖子

如何在一段时间后清除 Redux Persist Store

在我的 React 应用程序中,我希望用户的购物车和数据在一定时间后被清除。

我尝试使用 setTimeout() 函数,该函数在 1 小时后运行 persistor.purge() ,但是当您关闭浏览器选项卡时,此方法不起作用。因此,如果用户在几天后再次打开网站,商店数据仍保留在那里。

第二种方法,我尝试了(redux-persist-expire)包,但网站没有任何变化。我的代码,index.js 文件,

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;
Run Code Online (Sandbox Code Playgroud)

代码中是否需要修复某些内容?或者有更好的方法来处理它。

reactjs react-redux redux-persist

5
推荐指数
1
解决办法
6330
查看次数

标签 统计

react-redux ×1

reactjs ×1

redux-persist ×1