javascript - Redux/toolkit 在注销时重置所有状态,除了一个
问题描述
我想重置我所有的 redux 对象,除了一个(目前)。我正在使用的代码正在运行,但我认为它效率不高,因为每次添加新切片时,我都必须更新以下代码:
// app reducers
const combinedReducer = combineReducers({
auth,
general,
books,
authors,
events
});
// reducers type
export type AppReducerType = ReturnType<typeof combinedReducer>;
// root reducer
const rootReducer = (rootState: AppReducerType | undefined, action: AnyAction) => {
// terminate the state of a redux store
if (action.type === 'Auth/terminate') {
if (rootState) {
rootState = {
...rootState,
auth: aState, // reset state
general: rootState.general, // keep it as it is
books: bState, // reset state
authors: auState, // reset state
events: eState, // reset state
};
}
}
return combinedReducer(rootState, action);
};
export default rootReducer;
如果我使用rootState = undefined
,那么它会重置所有状态,包括general
我不想要的状态。有没有更好的方法来实现上述功能?
解决方案
一般来说,您的选择是:
- 让每个单独的切片重置自己的状态以响应相同的操作
- 保留初始状态的副本,并执行以下操作:
return {
...initialRootState,
auth: state.auth
}
推荐阅读
- javascript - 删除 QueryString 参数?
- java - 是否可以在过滤器中更新 Bean 属性?
- swift - 使用 AutoLayout 使用 UILabel 屏蔽 UIView
- ios - Xcode 10 无法在设备上运行 XCTest/XCUITest
- python - Python - 如何删除选定列表框项目周围的边框?
- ios - 将从@escaping收到的值快速分配给另一个类中的实例变量
- html - CSS Grid 2 行之间的间隙,没有间隙或填充等
- clojure - LazySeq 不能在 sort-by 上强制转换为 Comparable
- c - 为什么没有 INT_BIT?
- java - Elasticsearch REST 客户端 MultiGet——GetResult 始终为空