在Redux-Toolkit中使用Combine Reducer重置嵌套状态的最佳方法

时间:2020-05-13 20:09:25

标签: reactjs typescript react-redux redux-toolkit

说我的商店有下面的嵌套CombineReducers

const rootReducer = () =>
  combineReducers({
    agent,
    customer: customerReducer
  });

const customerReducer = combineReducer({
  profile,
  account
})

在这种情况下,如果我想将完整的客户状态重置为其初始状态,怎么办?

注意:

"@reduxjs/toolkit": "^1.3.5",
"typescript": "^3.8.3"

1 个答案:

答案 0 :(得分:1)

商店配置:

假设这是使用reduxjs/toolkit 配置您的商店的方式:

const store = configureStore({
  reducer: rootReducer,
  middleware: [
    ...getDefaultMiddleware(),
  ],
})

export default store

其中rootReducer是:

const rootReducer = combineReducers({
  agent: agentReducer,
  customer: customerReducer
});

其中customerReducer嵌套的地方,即其他两个减速器的组合<< / em>:

const customerReducer = combineReducer({
  profile,
  account
})

答案:

要仅在Redux Store中{strong>重置状态,您需要创建一个 wrapper 减速器,将操作简单地委派给您的{{1} },并且只要该包装减速器收到customer类型的操作,它都会重置客户状态

customerReducer

看到:How to reset redux state