我在我的React应用程序中使用redux-persist。注销后,我想完全重置redux状态并删除所有本地存储数据。
这是我的注销操作,其中我从后端删除了刷新令牌,然后在调度RESET之前在redux存储持久器上调用purge。
export const logout = () => (dispatch) => {
if (localStorage.refreshToken) {
axios.post(`${API_URL}/logout`, {token: localStorage.refreshToken}).then( async () => {
// rest redux-persist
await persistor.purge();
dispatch({ type: RESET });
});
}
}
在我的root减速器中,我遵循this answer here来处理此RESET动作:
const appReducer = combineReducers({
/* app level reducers...*/
});
const rootReducer = (state, action) => {
console.log("Reducing action: ", action);
if (action.type === RESET) {
// reset state
state = undefined;
// reset local storage
localStorage.clear();
}
return appReducer(state, action)
}
export default rootReducer;
我可以看到我的令牌已从本地存储中清除,并且最后触发的动作确实是RESET动作。但是本地存储仍然包含来自redux-persist的persist:root
。有人知道为什么不删除它,或者我如何实现我想做的事情吗?