使用 redux-persist 清除后变得 undefined not an object

getting undefined not an object after purging with redux-persist

我正在将过期令牌存储到 redux-persist 中以进行身份​​验证。我用了 persistor.purge();在商店上重置 redux 状态。现在,当我加载它时,我不断收到以下错误消息。我不知道如何解决这个问题?

Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'action.payload.auth')]
* reducers/auth_reducer.js:11:45 in default
- node_modules/redux/lib/combineReducers.js:133:36 in combination
- node_modules/redux-persist/lib/persistReducer.js:117:39 in <unknown>
- node_modules/redux/lib/createStore.js:178:36 in dispatch
- node_modules/redux-persist/lib/persistStore.js:100:21 in rehydrate
- node_modules/redux-persist/lib/persistReducer.js:85:27 in <unknown>
- node_modules/promise/setimmediate/core.js:37:14 in tryCallOne
- node_modules/promise/setimmediate/core.js:123:25 in <unknown>
- node_modules/react-native/Libraries/Core/Timers/JSTimers.js:154:6 in _callTimer
- node_modules/react-native/Libraries/Core/Timers/JSTimers.js:202:17 in _callImmediatesPass
- node_modules/react-native/Libraries/Core/Timers/JSTimers.js:470:11 in callImmediates
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:275:4 in __callImmediates
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:140:6 in <unknown>
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:262:6 in __guard
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:139:17 in flushedQueue

这是我的商店代码:

import { createStore, applyMiddleware, compose } from 'redux';
import ReduxThunk from 'redux-thunk';
import { AsyncStorage } from 'react-native';
import { persistStore, persistCombineReducers } from 'redux-persist';
//  import Reactotron from 'reactotron-react-native';
// import { PersistGate } from 'redux-persist/lib/integration/react';
import reducers from '../reducers';

const config = {
  key: 'root',
  storage: AsyncStorage,
  whitelist: ['momentDuration', 'auth', 'storybook'],
};

//
config.debug = true;
const persistReducer = persistCombineReducers(config, reducers);

export default function configureStore() {
  const store = createStore(
    persistReducer,
    {},
    compose(applyMiddleware(ReduxThunk)),
  );
  //  add a .purge() below;
  const persistor = persistStore(store);
  //  persistor.purge();
  return { persistor, store };
}

在我的减速器中我正在使用

case 'persist/REHYDRATE':
  return ({ tokenExpires: action.payload.auth.tokenExpires } || {});

你试过类似的东西吗?

case 'persist/REHYDRATE':
  return ({ tokenExpires:  action.payload && action.payload.auth && action.payload.auth.tokenExpires } || {});

此外,请确保您的 action creator 中没有任何拼写错误,希望它能对您有所帮助