简体   繁体   English

State 被最后更新的值覆盖

[英]State is overriding with last updated value

I have below function where i am updating the array that is stored in state as array and i have 9 tables and for each table i am calling this function to check the status and if it is true then i will be storing that in array.我有以下 function ,我正在更新存储在 state 中的数组作为数组,我有 9 个表,对于每个表我都调用这个 ZC1C425268E68385D1AB5074C17A94F14,然后检查它是否存储在数组中。

const [projectSpaceTypeWarning, setProjectSpaceTypeWarning] = useState([]);

const handleWarning = (librarySourceTableWarning, status) => {
let spaceTypeWarningCount = [...projectSpaceTypeWarning];
if (status) {
  spaceTypeWarningCount.push(librarySourceTableWarning);
} else {
  spaceTypeWarningCount = spaceTypeWarningCount.filter(
    item => item !== librarySourceTableWarning
  );
 }
setProjectSpaceTypeWarning(spaceTypeWarningCount);
};

and i am setting another variable based in this state like as below我正在基于此 state 设置另一个变量,如下所示

 if (projectSpaceTypeWarning.length) {
  values.spaceType.isInWarningState = true;
}

My issue here is i am updating this state with one table that is giving status as true and it is setting the state accordingly and that is fine.我的问题是我正在更新这个 state,其中一张表的status为 true,并且它正在相应地设置 state,这很好。 I have remaining tables those are all sending the status as false and setting the status accordingly.我还有剩余的表,它们都将状态发送为 false 并相应地设置状态。 Somehow the false status is overriding true status inside the state array.不知何故, false状态覆盖了 state 阵列中的true状态。

Could any one please let me know any suggestions or any ideas how i can overcome this problem.任何人都可以让我知道如何解决这个问题的任何建议或想法。

Many thanks in advance.提前谢谢了。

I have solved this problem by using below code我已经通过使用下面的代码解决了这个问题

    const handleWarning = (librarySourceTableWarning, status) => {
    let spaceTypeWarningCount = projectSpaceTypeWarning; // changes at here
    if (status) {
      spaceTypeWarningCount.push(librarySourceTableWarning);
    } else {
      spaceTypeWarningCount = spaceTypeWarningCount.filter(
        item => item !== librarySourceTableWarning
      );
    }
    setProjectSpaceTypeWarning(spaceTypeWarningCount);
   };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM