next.js React useState将有效的动态对象转换为NaN值[已关闭]

6qfn3psc  于 2023-08-04  发布在  React
关注(0)|答案(1)|浏览(65)

**已关闭。**此问题需要debugging details。它目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答这个问题。
6天前关门了。
Improve this question
我从一个时间序列数据库导入,该数据库返回一个对象在一小时内处于不同状态的毫秒数。数据看起来像下面。

{
  id: mejfa24191@$kr,
  timestamp: 2023-07-25T12:51:24.000Z,
  // This is the field that is dynamic
  meta: {
    active: 360000,
    stopped: 710000,
    idle: 0,
    etc.
  }
}

字符串
我想对每个小时的所有这些值取一些,以获得每个字段的总时间。为此,我检查何时找到新的小时,并从前一个条目中获取值,因为这将是前一个小时顶部的条目。
我有一个问题,我能够创建一个有效的对象,从前一个小时的总时间,但每当我设置为一个状态变量,它把有效的对象变成一个每个数字是NaN。
我目前正在尝试使用深层副本来设置状态。最初,我尝试在for循环中使用函数设置为setTotalHours((prev) => ({...prev, [key]: totalHours[key] + currHourTotal[key]}))
代码如下:

const [totalHours, setTotalHours] = useState<{[key: string]: number}>({});
  useEffect(() => {
    if (!loading) {
      let currHourTotal: { [key: string]: number } = {};
      let currHour = new Date(hours[0].timestamp).getUTCHours();
      for (const h of hours) {
        if (new Date(h.timestamp).getUTCHours() !== currHour) {
          if (h.meta) {
            const total = cloneDeep(totalHours);
            for (const key in h.meta) {
              total[key] = total[key]
                ? total[key] + Number((currHourTotal[key] / (1000 * 60 * 60)).toFixed(2))
                : Number((currHourTotal[key] / (1000 * 60 * 60)).toFixed(2));
            }
            console.log('Total Deep Copy', total);
            setTotalHours(total);
            console.log('Total State', totalHours);

          }
          currHour = new Date(h.timestamp).getUTCHours();
        } else if (new Date(h.timestamp).getUTCHours() === currHour) {
          if (h.meta) {
            for (const key in h.meta) {
              currHourTotal[key] = h.meta[key];
            }
          }
        }
      }
      const total = cloneDeep(totalHours);
      for (const key in currHourTotal) {
        total[key] = total[key] + currHourTotal[key] / (1000 * 60 * 60);
      }
      setTotalHours(total);
    }
  }, [loading, hours]);


如果我看看上面列出的日志,它看起来像这样。
Logs from run
我试过使用函数设置,如setTotalHours((prev) => ({...prev, [key]: prev[key] + currHourTotal[key]}))和其他方式,但我所尝试的一切都导致了这个失败。
这让我很生气,所以任何帮助都很感激。

ct2axkht

ct2axkht1#

这里有一个使用reducer来计算“total”对象的方法。

const hours = [
{
  id: "mejfa24191@$kr",
  timestamp: "2023-07-25T12:51:24.000Z",
  // This is the field that is dynamic
  meta: {
    active: 360000,
    stopped: 710000,
    idle: 0
  }
},
{
  id: "mejfa24191@$ks",
  timestamp: "2023-07-25T12:51:24.000Z",
  // This is the field that is dynamic
  meta: {
    active: 360000,
    stopped: 710000,
    idle: 0,
    somethingElse: 200
  }
},
{
  id: "mejfa24191@$kt",
  timestamp: "2023-07-25T13:51:24.000Z",
  // This is the field that is dynamic
  meta: {
    active: 360000,
    stopped: 710000,
    idle: 0,
    other: 200
  }
}
];

const total = hours.reduce((acc, el) => {
  const h = new Date(el.timestamp).getUTCHours();
  if (!acc[h]) {
    acc[h] = {...el.meta};
  } else {
    Object.keys(el.meta).forEach(key => {
      if (!acc[h][key]) 
        acc[h][key] = el.meta[key];
      else
        acc[h][key] += el.meta[key];
    });
  }
  return acc;
}, {});

console.log(total);

字符串

相关问题