动机
我想在页面刷新/重定向时保留用户信息,但我也想在注销操作后清除它。
配置
const rootReducer = combineReducers({productsReducer, auth: accessReducer, usersReducer});
const persistConfig = {
key: 'root',
storage,
whitelist: ['auth']
};
我只想保留数据,accessReducer
这就是它被列入白名单的原因。现在我想以重置所有数据的方式处理注销。为此,我创建了 logoutReducer:
注销减速器
export const logout = () => ({
type: 'USER_LOGOUT'
});
const logoutReducer = (state, action) => {
if(action.type = 'USER_LOGOUT'){
state = undefined;
}
return rootReducer(state, action);
};
存储和持久化
const persistedReducer = persistReducer(persistConfig, logoutReducer);
export const store = createStore(persistedReducer, applyMiddleware(thunk));
export const persistor = persistStore(store);
问题
运行应用程序后,我会获得来自<Loader/>
in的加载信息PersistGate
:
<Provider store={store}>
<PersistGate loading={<Loader/>} persistor={persistor}>
...
</PersistGate>
</Provider>
可能的原因
现在我想这是因为我通过logoutReducer
了它只引用了其他减速器,这就是配置失败的原因。如何保留用户信息并保留注销清除存储技巧?有没有其他解决方案?任何帮助将不胜感激♥