【问题标题】:How might I reset state of just one reducer when I have multiple reducers and prevent reload on state reset of just one reducer当我有多个减速器时,如何仅重置一个减速器的状态并防止在仅一个减速器的状态重置时重新加载
【发布时间】:2019-11-23 09:22:35
【问题描述】:

我将用户在表单中输入的数据保持在减速器的状态。我还有另一个减速器,它的状态与其他一些功能相关。现在我有一个表单的重置按钮,点击它,我想要状态更新只有 formReducer 而不是其他 reducer 的状态。另外我不希望我的应用在 formReducer 的状态重置时重新加载

已经添加了我用来实现该功能的代码 sn-p,但是会发生的是整个应用程序在单击重置时重新加载,并且两个减速器的状态最终都会重置 我的 index.js 有以下代码

   const appReducers = combineReducers({
       r1: reducer1, 
       formSubmissionReducer: formSubmissionReducer
       });
   const rootReducer = (state, action) => {

      if (action.type === "RESET_FORM") {
        const { r1 } = state;
        console.log(r1);
        state = { r1};

        }

      return appReducers(state, action);
    };

【问题讨论】:

标签: javascript reactjs react-redux reducers


【解决方案1】:

你必须分离你的减速器,现在当你得到 RESET_FORM 动作时,只是 formSubmissionReducer 会对此做出反应,reducer1 只会返回状态。 阅读redux docs,他们比我解释得更好。

const reducer1 = (state, action) => {

      ... // swtich statment
      return state;
    };
    
    const formSubmissionReducer = (state, action) => {

      if (action.type === "RESET_FORM") {
        // it will update the form variable inside the `formSubmissionReducer` state
        return state = {...state, form: action.payload};
      }

      return state;
    };
    
    
   const rootReducer = combineReducers({
       r1: reducer1, 
       formSubmissionReducer: formSubmissionReducer
   });

【讨论】:

  • 我已经分离了reducers。有2个文件,即reducer1.js和formSubmisiionReducer.js。我在index.js中合并了它们。
  • 创建一个代码笔来显示您的问题,并且很容易为您提供帮助
猜你喜欢
  • 2017-07-03
  • 2019-04-20
  • 1970-01-01
  • 2019-02-14
  • 2018-08-08
  • 2021-10-13
  • 2021-07-18
  • 1970-01-01
相关资源
最近更新 更多