我想保留页面刷新/重定向上的用户信息,但我也想在注销操作后清除它。
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/>
中的PersistGate
的加载信息:
<Provider store={store}>
<PersistGate loading={<Loader/>} persistor={persistor}>
...
</PersistGate>
</Provider>
现在我想是因为我通过了logoutReducer
,而该参考仅引用其他reducer,这就是配置失败的原因。如何保持用户信息并保持退出清除技巧?还有其他解决方案吗?任何帮助将不胜感激♥
答案 0 :(得分:0)
在研究此问题期间,我发现了this个问题,其中发帖人也有类似的问题。解决方案在这篇文章的最后一条评论中,即: react-reset 。 这是我对上述问题的解决方案:
const appReducer = combineReducers({productsReducer, auth: accessReducer, usersReducer});
const persistConfig = {
key: 'root',
storage,
whitelist: ['auth']
};
const enHanceCreateStore = compose(
applyMiddleware(thunk),
reduxReset()
)(createStore);
const persistedReducer = persistReducer(persistConfig, appReducer);
export const store = enHanceCreateStore(persistedReducer);
export const persistor = persistStore(store);
然后,您只需执行redux-reset
发出的注销调度操作:
store.dispatch({
type: 'RESET'
})