React对象的值减少了2次,而不是1次。

huangapple go评论62阅读模式
英文:

React object value decreased 2 times insead of 1

问题

I'm facing a non-logical problem!

我遇到了一个不合逻辑的问题!

I have an object and I'm trying to update a value nested too deep inside this object. The value is a type of number. I want to decrease the number by one for each click, but when I try to do this on that object, it decreases by two for each click.

我有一个对象,我试图更新其中一个嵌套很深的值。这个值是一个数字类型。我想每次点击时将数字减少一,但当我在这个对象上尝试时,每次点击它减少了两次。

This is my code:

这是我的代码:

const [newDhomatData, setNewDhomatData] = useState(() => {
    const obj = { ...dhomatFirebase[0] };

    for (let i = 1; i <= ditQendrimiArray.length; i++) {
        obj.muajt[`${muaj}`].datatDhomat[`dat${i}`][`${dataForSpecific.dhoma}`] -= 1;
    }
    return obj;
});

And this is the object:

这是对象:

React对象的值减少了2次,而不是1次。

So for example, when I run the code above, the value of let's say dhomCift, instead of being 44, it becomes 43!

所以例如,当我运行上面的代码时,dhomCift 的值,不是变成了 44,而是变成了 43!

The path of the object is correct, but I have been trying to solve this for about 1 hour, and it doesn't seem logical because I'm decreasing the value of this object key by 1 every click, but it gets decreased by two.

对象的路径是正确的,但我已经尝试解决这个问题大约1小时了,这看起来不合逻辑,因为我每次点击都将这个对象键的值减少1,但它减少了两次。

Thanks very much if someone will help.

非常感谢,如果有人愿意帮助的话。

英文:

so I'm facing a non logical problem!

I have an object and i'm trying to update a value nested too deep inside this object.The value is type of number. I want to decrease the number by one for click, but when i try to do this on that object it decreases by two for click.

This is my code:

const [newDhomatData, setNewDhomatData] = useState(() =&gt; {
const obj = { ...dhomatFirebase[0] };

for (let i = 1; i &lt;= ditQendrimiArray.length; i++) {
 obj.muajt[`${muaj}`].datatDhomat[`dat${i}`][`${dataForSpecific.dhoma}`] -= 1; 
}
return obj;
});

And this is the object:

React对象的值减少了2次,而不是1次。

So for example when i run the code above the value of let's say dhomCift instead of being 44 it becomes 43!

The path of object is right but I have about 1 hour trying to solve this thing and it looks not logical because I'm decreasing the value of this object key by 1 every click but it get decreased by two.

Thanks very much if someone will help.

答案1

得分: 2

这可能是由包装在您的应用程序周围的StrictMode组件引起的。您可能会在主要的App.js文件或ReactDOM的渲染方法中找到它。

Strict Mode启用了额外的开发行为和警告,用于其内部的组件树,并导致某些钩子运行两次。

有关更多信息,请查看此处

英文:

This is probably caused by a StrictMode component which is wrapping your application. You might find it in your main App.js file or in the ReactDOM render method.

Strict Mode enables additional development behaviors and warnings for the component tree inside it and causes some hooks to run twice.

More info about it here.

答案2

得分: 0

我找到了一个替代方法,可以在不出现问题的情况下完成这个操作。

const [newDhomatData, setNewDhomatData] = useState(() => {
  const obj = {
    ...dhomatFirebase[0],
    muajt: {
      ...dhomatFirebase[0].muajt,
      qershor: {
        datatDhomat: {
          ...dhomatFirebase[0].muajt.qershor.datatDhomat,
          dat1: {
            ...dhomatFirebase[0].muajt.qershor.datatDhomat.dat1,
            dhomFamiljare:
              dhomatFirebase[0].muajt.qershor.datatDhomat.dat1.dhomFamiljare - 1,
          },
        },
      },
    },
  };
  return obj;
})

与在声明对象后更新对象不同,我可以在声明对象的同时更新对象,这将给我想要的结果!

这可能对某人有帮助。

英文:

I found an alternative way of doing this without getting problems

const [newDhomatData, setNewDhomatData] = useState(() =&gt; {
const obj = {
  ...dhomatFirebase[0],
  muajt: {
    ...dhomatFirebase[0].muajt,
    qershor: {
      datatDhomat: {
        ...dhomatFirebase[0].muajt.qershor.datatDhomat,
        dat1: {
          ...dhomatFirebase[0].muajt.qershor.datatDhomat.dat1,
          dhomFamiljare:
            dhomatFirebase[0].muajt.qershor.datatDhomat.dat1.dhomFamiljare -
            1,
        },
      },
    },
  },
};
return obj;

})

instead of updating the object after declaring it i can update the object on the time that i will declare it and it will give me the result that i want!

This may be helpfull to someone.

huangapple
  • 本文由 发表于 2023年6月9日 05:57:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定