redux persist:注销时未清除根localstorate数据

omtl5h9j  于 2022-11-12  发布在  其他
关注(0)|答案(1)|浏览(264)

Reactjs和redux来创建应用程序。我已经将reducer数据连接到localStorage persist:root变量。现在尝试在注销时删除persist:root,但是失败了。尝试了很多来自Web的解决方案,现在代码看起来像下面这样。但是仍然不工作。请帮助。
异径管:

import {applyMiddleware, createStore} from 'redux';
import createSagaMiddleware from 'redux-saga';
import {persistStore, persistReducer} from 'redux-persist';
import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2';
import storage from 'redux-persist/lib/storage';
import logger from 'redux-logger';
// eslint-disable-next-line import/no-cycle
import rootReducer from '../redux/reducers';
// eslint-disable-next-line import/no-cycle
import rootSaga from '../redux/sagas';

const persistConfig = {
  key: 'root',
  storage,
  stateReconciler: autoMergeLevel2
};

const bindMiddleware = (middleware) => {
  return applyMiddleware(...middleware);
};

const sagaMiddleware = createSagaMiddleware();

export const store = createStore(
  persistReducer(persistConfig, rootReducer),
  bindMiddleware([sagaMiddleware])
);
sagaMiddleware.run(rootSaga);

export const persistor = persistStore(store);

商店:

import { combineReducers } from "redux";
import commonReducer from "./common";
import loginReducer from "./auth/login";
import companyReducer from "./company"
import processReducer from "./process"
import notificationReducer from "./notification"
import recentProgressReducer from "./recentProgress"
import projectManagementReducer from "./projectManagement";
import stepManagementReducer from "./stepManagement";
import memberManagementReducer from "./memberManagement";

const appReducer = combineReducers({
  commonReducer,
  processReducer,
  companyReducer,
  notificationReducer,
  loginReducer,
  recentProgressReducer,
  projectManagementReducer,
  stepManagementReducer,
  memberManagementReducer
});
export default  (state, action) => {
  if (action.type === "SIGNOUT_REQUEST") {
      localStorage.removeItem('persist:root')
      return appReducer(undefined, action);
  }else{
    return appReducer(state, action);
  }
};

这是行不通的。请帮助

2hh7jdfx

2hh7jdfx1#

persist:root不会被删除,因为“redux-persist”需要它来检索。不要试图删除它,只需要像使用appReducer(undefined,action)那样将状态置空即可;。

相关问题