我使用redux-persist将我的状态保存在本地存储中,我需要在 5 分钟后注销用户并将用户移动到登录页面,但是每次尝试时我都无法从本地存储中删除持久值使用storage.removeItem删除值,redux-persist 返回值。
根减速器:
import storage from 'redux-persist/lib/storage';
const appReducer = combineReducers({
auth: authReducer,
//....
});
const rootReducer = (state, action) => {
if (action.type === SIGNOUT_REQUEST) {
Object.keys(state).forEach(key => {
storage.removeItem(`persist:${key}`);
});
state = undefined;
}
return appReducer(state, action);
}
export default rootReducer;
注销操作:
export const logoutUser = () => dispatch => {
dispatch({
type: SIGNOUT_REQUEST
})
APP.js
if (isTimeout) {
store.dispatch(logoutUser());
}
index.js
ReactDom.render(
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<App />
</PersistGate>
</Provider>
, document.querySelector('#root')
);