Javascript 为什么返回的时间戳值在React中得到bool-inside-useffect?

Javascript 为什么返回的时间戳值在React中得到bool-inside-useffect?,javascript,reactjs,timestamp,Javascript,Reactjs,Timestamp,我正在尝试使用子字符串从时间戳值中仅获取年份。 时间戳值类似于2020-11-19T17:26:53.561Z。 在我的例子中,您可以看到下面的代码,paid_date是时间戳值 const invoices = [ {paid_date: "2020-11-19T17:26:53.561Z", amount: 50}, ... ]; useEffect(() => { const new = invoices.map(invoic

我正在尝试使用
子字符串
从时间戳值中仅获取年份。 时间戳值类似于
2020-11-19T17:26:53.561Z
。 在我的例子中,您可以看到下面的代码,
paid_date
是时间戳值

  const invoices = [
    {paid_date: "2020-11-19T17:26:53.561Z", amount: 50},
    ...
  ];

  useEffect(() => {
    const new = invoices.map(invoice => ({
      paid_year: +invoice.paid_date.substr(0, 4),
      paid_month: +invoice.paid_date.substr(5, 2),
      amount: +invoice.amount
    }))

    console.log(new);
  }, [invoices, isMountedRef]);
我预计结果是2020年这样的付费年,但它返回以下数组

0: {paid_year: 2020, paid_month: 11, amount: 240}
1: {paid_year: false, paid_month: 11, amount: 500}
2: {paid_year: false, paid_month: 11, amount: 500}
3: {paid_year: true, paid_month: 12, amount: 20}
4: {paid_year: false, paid_month: 5, amount: 10}
5: {paid_year: false, paid_month: 3, amount: 5}
...
为什么它返回的第一个
支付年
2020
,而其他的是
boolean
? 我尝试使用
substr
substring
slice
方法。 奇怪的是,结果是正确的,我只预期了几次

更新:
新的
变量名只是我的想法,所以这不是重点。 无论我使用的名称是什么,结果都是一样的,而不是
new

发票
是axios api响应,来自我的PostgreSQL数据库

我不确定这是否是因为错误使用了
useffect


它在
useffect

之外运行得很好。我的兄弟,在react中,你可以用多种方式处理日期。如果你有很多事情和日期有关,那么moment.js是最好的软件包

以下是链接:

do安装:npm安装时刻

这里有一个用例

从“时刻”导入时刻
常量发票=[
{支付日期:“2020-11-19T17:26:53.561Z”,金额:50},
...
];
useffect(()=>{
const new=invoices.map(invoices=>({
付款年份:+发票.付款日期.时间.格式('YYYY'),
付款月份:+发票.付款日期.时间.格式('M'),
金额:+发票金额
}))
控制台日志(新);

},[发票,isMountedRef]
const new
new
是js中的保留字。请给出一个返回非预期值的示例数据result@AlanOmar,
新的
或任何总是一样的东西。我认为这与反应无关,但如果你能提供完整的
发票
,这将帮助我们看到问题。你为什么不阅读全文?这不是作者想要的,事实上