商店配置:
假设这是使用reduxjs/toolkit配置商店的方式:
const store = configureStore({
reducer: rootReducer,
middleware: [
...getDefaultMiddleware(),
],
})
export default store
在哪里rootReducer
:
const rootReducer = combineReducers({
agent: agentReducer,
customer: customerReducer
});
并且嵌套在哪里,即customerReducer
其他两个减速器的组合:
const customerReducer = combineReducer({
profile,
account
})
回答:
要仅重置customer
Redux Store 中的状态,您需要创建一个包装减速器,它将简单地将操作委托给您customerReducer
,并且每当此包装减速器收到一种CUSTOMER/RESET
类型的操作时,它都会重置客户状态:
const customerReducerWrapper = (state, action) => {
if (action.type === 'CUSTOMER/RESET') {
state = undefined
}
return customerReducer(state, action)
}
const rootReducer = combineReducers({
agent: agentReducer,
customer: customerReducerWrapper
});
请参阅:如何重置 redux 状态?