如何重置Redux存儲的狀態?我正在使用Redux進行州管理。如何將存儲重置為初始狀態?例如,假設我有兩個用戶帳戶(u1和u2).設想以下一系列事件:用戶u1登錄應用程序并做一些事情,所以我們在商店中緩存一些數據。用戶u1注銷。用戶u2登錄到應用程序而不刷新瀏覽器。此時,緩存的數據將與u1我想把它清理干凈。當第一個用戶注銷時,我如何將Redux存儲重置為它的初始狀態?
3 回答

MM們
TA貢獻1886條經驗 獲得超2個贊
undefined
const rootReducer = (state, action) => { if (action.type === 'USER_LOGOUT') { const { routing } = state state = { routing } } return appReducer(state, action)}

慕沐林林
TA貢獻2016條經驗 獲得超9個贊
const RESET_ACTION = { type: "RESET"}
switch
if-else
switch
.
const INITIAL_STATE = { loggedIn: true}const randomReducer = (state=INITIAL_STATE, action) { switch(action.type) { case 'SOME_ACTION_TYPE': //do something with it case "RESET": return INITIAL_STATE; //Always return the initial state default: return state; }}
RESET
const logoutHandler = () => { store.dispatch(RESET_ACTION) // Also the custom logic like for the rest of the logout handler}
store.dispatch(RESET_ACTION)
LOGOUT_ACTION
.
LOGOUT_ACTION
添加回答
舉報
0/150
提交
取消