场景是用户相关的详细信息存储在一个减速器中。当用户 A 注销设备而用户 B 登录设备时,数据将被擦除,新用户数据将存储在减速器中。但是当应用程序关闭并从退出状态启动时,用户A的数据从persist中存储到reducer中。
我的商店配置代码如下:
import {createStore, compose, applyMiddleware} from 'redux';
import {persistStore, persistCombineReducers} from 'redux-persist';
import AsyncStorage from '@react-native-community/async-storage';
import {createLogger} from 'redux-logger';
import createSagaMiddleware from 'redux-saga';
import rootReducers from '../reducers'; // where reducers is a object of reducers
import sagas from '../sagas';
const config = {
key: 'root',
storage: AsyncStorage,
blacklist: ['loadingReducer'],
debug: true, //to get useful logging
};
const middleware = [];
const sagaMiddleware = createSagaMiddleware();
middleware.push(sagaMiddleware);
if (__DEV__) {
middleware.push(createLogger());
}
const reducers = persistCombineReducers(config, rootReducers);
const rootReducer = (state, action) => {
if (action.type === 'LOG_OUT') {
return reducers(undefined, action);
}
return reducers(state, action);
};
const enhancers = [applyMiddleware(...middleware)];
const persistConfig = {enhancers};
const store = createStore(rootReducer, undefined, compose(...enhancers));
const persistor = persistStore(store, persistConfig, () => {
// console.log('Test', store.getState());
});
const configureStore = () => {
return {persistor, store};
};
sagaMiddleware.run(sagas);
export default configureStore;
我尝试在用户尝试使用 AsyncStorage.removeItem('persist:root')
时使用 LOG_OUT
,但没有改变。旧数据即使应该被覆盖也被持久化。
知道是什么造成了这种奇怪的情况吗??