如何阻止redux-persit自动重新水化redux-form的“ form”状态

Hun*_*yen 5 reactjs redux redux-form

我使用的是redux-form,它提供了一个内置的reducer,称为“ formReducer”,需要在组合的reducer中进行注册,以通过redux的存储来管理表单状态。

我还使用redux-persist来保存redux存储。

当我不想让我的表单自动重新填充用户在页面重新加载或页面刷新时输入的数据时出现的问题。在我自己编写的普通reducer中,我可以简单地为“ REHYDRATE”类型的动作添加一个切换用例(由redux-persit调度),以防止状态片通过返回其初始状态或空状态而自动补水。但是redux-form的formReducer是redux-form内置的,所以我不能更改。那么,有什么方法可以“定制” redux形式的reduce来添加该开关盒吗?或者,有什么方法可以配置redux-persist不自动重新水化特定状态切片,或者有什么方法可以配置redux-form以不通过页面重新加载或页面刷新自动填充?

Sag*_*b.g 1

您可以使用中间件来处理此特定操作类型并防止其传递给减速器。

const myMiddleWare = store => next => action => {
  if(action.type != 'REHYDRATE'){
     next(action); // pass the action forward to the reducers
  } else{
    // do your logic here, you can use store.dispatch to dispatch other actions
    // when your not invoking  next(action) this action won't pass through to all the reducers
  }
}
Run Code Online (Sandbox Code Playgroud)