在Reducer中反应对象的本机更新数组

时间:2020-07-14 14:59:47

标签: react-native redux

在我的本地本机应用程序中,我处于这样的状态

var data = [
{
   "ChildFirstName":"Cgi",
   "ChildLastName":"I-Cgi",
   "ChildKey":"aff5749a-84cf-43a0-a477-8cc47e0c49c5",
   "LstParentInfo":[
      {
         "ParentKey":"14338e45-8759-45e7-1358-59528e2a7cde",
         "ParentFirstName":"Tahir Mahmood",
         "ParentLastName":"Khawaja",
         "ParentEmail":"",
         "ParentMobile":"",
         "ParentAddress":"Tøyengata 1, 0190 Oslo",
         "IsSelected":false
      },
      {
         "ParentKey":"13736d0d-8764-05aa-de4a-d1aaeb087cc8",
         "ParentFirstName":"Suayip",
         "ParentLastName":"Eryigit",
         "ParentEmail":"",
         "ParentMobile":"",
         "ParentAddress":"Stovner Senter 18, 0985 Oslo",
         "IsSelected":false
      }
   ]
}
];

用户单击父项,我要将父项的值设置为IsSelected:!IsSelected。我正在这样的减速器中尝试这个

case ActionTypes.UPDATE_SELECTED_CLASS_LIST:
  var parent = action.parent;
  var classList = [...state.classList];


  const childToBeUpdated = classList.find((i) =>
    i.LstParentInfo.filter((k) => k.ParentKey == parent.ParentKey)
  );


  const parentToBeUpdated = childToBeUpdated.LstParentInfo.find(
    (k) => k.ParentKey == parent.ParentKey
  );
  parentToBeUpdated.IsSelected = !parentToBeUpdated.IsSelected;


 var data = updateObject(classList, { [0]: childToBeUpdated });
  
 classList = Object.values(childToBeUpdated);
  

  console.log("[Update_Class_final]", data);

  return {
    ...state,
    classList: classList,
  };

export const updateObject = (oldObject, updatedProperties) => {
 return {
...oldObject,
...updatedProperties,
};
};

但这不起作用,有人可以帮我更新状态吗?

0 个答案:

没有答案