我已经看到了注销后清除/重置存储的解决方案,但不了解如何通过以下方式来实现相同的功能来设置Redux存储。
Store.js:
import { configureStore, getDefaultMiddleware } from '@reduxjs/toolkit' import authReducer from './ducks/authentication' import snackbar from './ducks/snackbar' import sidebar from './ducks/sidebar' import global from './ducks/global' import quickView from './ducks/quickView' import profileView from './ducks/profileView' const store = configureStore({ reducer: { auth: authReducer, snackbar, sidebar, global, quickView, profileView, }, middleware: [...getDefaultMiddleware()], }) export default store
这是使用@ reduxjs / toolkit中的createAction和createReducer实现所有化简器的方法。>>
snackbar.js:
import { createAction, createReducer } from '@reduxjs/toolkit' export const handleSnackbar = createAction('snackbar/handleSnackbar') export const openSnackBar = ( verticalPosition, horizontalPosition, message, messageType, autoHideDuration = 10000 ) => { return async dispatch => { dispatch( handleSnackbar({ verticalPosition, horizontalPosition, message, autoHideDuration, messageType, isOpen: true, }) ) } } export const closeSnackbar = () => { return dispatch => { dispatch(handleSnackbar({ isOpen: false })) } } const initialState = { verticalPosition: 'bottom', horizontalPosition: 'center', message: '', autoHideDuration: 6000, isOpen: false, messageType: 'success', } export default createReducer(initialState, { [handleSnackbar]: (state, action) => { const { isOpen, verticalPosition, horizontalPosition, message, autoHideDuration, messageType, } = action.payload state.isOpen = isOpen state.verticalPosition = verticalPosition state.horizontalPosition = horizontalPosition state.message = message state.autoHideDuration = autoHideDuration state.messageType = messageType }, })
我已经看到了注销后清除/重置存储的解决方案,但是不了解如何为以下设置redux存储的方式实现相同的功能。 Store.js:...
带有两个减速器的简化示例:
// actions and reducer for state.first
const resetFirst = () => ({ type: 'FIRST/RESET' });
const firstReducer = (state = initialState, action) => {
switch (action.type) {
// other action types here
case 'FIRST/RESET':
return initialState;
default:
return state;
}
};
// actions and reducer for state.second
const resetSecond = () => ({ type: 'SECOND/RESET' });
const secondReducer = (state = initialState, action) => {
switch (action.type) {
// other action types here
case 'SECOND/RESET':
return initialState;
default:
return state;
}
};
const rootReducer = combineReducers({
first: firstReducer,
second: secondReducer
});
// thunk action to do global logout
const logout = () => (dispatch) => {
// do other logout stuff here, for example logging out user with backend, etc..
dispatch(resetFirst());
dispatch(resetSecond());
// Let every one of your reducers reset here.
};