在Redux状态下更新每个对象中的特定属性

时间:2020-02-04 06:20:41

标签: reactjs redux reducers

我想在Redux状态下更新每个对象的特定属性,并假设我具有这种主要状态

networkFailure: true
employer: {
  signUp: {
    data: null
    error: null
    loading: true
  }
}
user: {
  signUp: {
    data: null
    error: null
    loading: false
  }
}

假设我遇到了网络故障,然后 networkFailure 状态变为 true ,因此我需要失败(假)加载 signUp 雇主的“强”状态以及所有其他对象的“ ”值都处于加载状态

是否可以在一个减速器中执行此操作?或有任何更好的建议吗? 我为雇主,用户和网络设置了3个reducer功能

2 个答案:

答案 0 :(得分:0)

我认为有两种方法: 1.使用redux-thunk并分派一个动作,然后在该动作函数内分派3个动作,这些动作会将分派函数返回给您。

function setErrorAction() {
  return dispatch => {
      const networkError = {type: "network/singup/error"}
      const employer Error = {type: "employer/singup/error"}
      const userError = {type: "user/singup/error"}

      dispatch(networkError)
      dispatch(employer)
      dispatch(userError)
  }
}
  1. 编写一个可以访问分派功能的钩子函数,该函数可以公开该函数以分派动作,当您调用该钩子函数时,它将分派动作给reducer,但这是一种集中业务的方法。像这样的东西:
import {useDispatch} from "react-redux";

function useAuthError() {
   const dispatch = useDispatch();

   function setError() {
      const networkError = {type: "network/singup/error"}
      const employer Error = {type: "employer/singup/error"}
      const userError = {type: "user/singup/error"}

      dispatch(networkError)
      dispatch(employer)
      dispatch(userError)
   };

   return {
      setError
   }
}

答案 1 :(得分:0)

here找到一个答案

实际上,当发生网络故障时,对我来说,重置应用程序状态是可以的,

所以我已经在root reducer中重置了应用程序状态

ID      Date  X  123_Var  456_Var  789_Var  123_Var_4  456_Var_4  789_Var_4
 A  16-07-19  3      777      250      810        NaN        NaN        NaN
 A  17-07-19  9      637      121      529      777.0      250.0      810.0
 A  20-07-19  2      295      272      490      637.0      121.0      529.0
 A  21-07-19  3      778      600      544      295.0      121.0      490.0
 A  22-07-19  6      741      792      907      295.0      272.0      490.0
 A  25-07-19  6      435      416      820      741.0      600.0      544.0
 A  26-07-19  8      590      455      342      435.0      416.0      820.0
 A  27-07-19  6      763      476      753      435.0      416.0      342.0
 A  02-08-19  6      717      211      454        NaN        NaN        NaN
 A  03-08-19  6      152      442      475      717.0      211.0      454.0
 A  05-08-19  6      564      340      302      152.0      211.0      454.0
 A  07-08-19  6      105      929      633      152.0      340.0      302.0
 A  08-08-19  6      948      366      586      105.0      340.0      302.0
 B  07-08-19  4      509      690      406        NaN        NaN        NaN
 B  08-08-19  2      413      725      414      509.0      690.0      406.0
 B  12-08-19  2      170      702      912      413.0      725.0      414.0
 B  13-08-19  3      851      616      477      170.0      702.0      414.0
 B  14-08-19  9      475      447      555      170.0      616.0      477.0
 B  15-08-19  1      412      403      708      170.0      447.0      477.0
 B  17-08-19  2      299      537      321      412.0      403.0      477.0
 B  18-08-19  4      310      119      125      299.0      403.0      321.0

这会将其他减速器的状态保持为默认状态