React hook 卡在先前的对象值上

发布于 2025-01-16 08:45:50 字数 486 浏览 0 评论 0原文

我有一个正在使用 useState 创建和更新的对象列表:

{
        id: list1.length + 1,
        key: list1.length + 1,
        variable: userInput.variable,
        name: userInput.name,
        value: userInput.value,
}

当我将 name: 的值设置为“text”时,该值会更改并更新为“variable”,这是以前常用的值。当我查看 Chrome 调试器时,该值从未改变并且是正确的“文本”,但是,当我 console.log() 时,它显示的值是“变量”。另一个奇怪的事情是,在我将一个项目添加到此对象列表中后,调试器将在名称中显示更改的值:对于列表中的先前项目。

最大的问题是我检查了所有代码并将“文本”硬编码到名称值中:你猜怎么着?它仍然显示为“变量”。 React 中有某种缓存吗?或者也许有一个错误?

I have a list of objects I am creating and updating with useState:

{
        id: list1.length + 1,
        key: list1.length + 1,
        variable: userInput.variable,
        name: userInput.name,
        value: userInput.value,
}

When I set the value of name: to "text", the value changes and updates to "variable", which is a value that has been previously and commonly used. When I looked through the Chrome debugger the value never changed and was correct, "text", however, when I console.log() the value it shows me "variable". Another strange thing is that after I add an item to this list of objects, the debugger will show a changed value in name: for the previous items in the list.

The biggest kicker is that I went through all my code and hard coded in "text" into the value for name: and guess what? It still shows as "variable". Is there some sort of cache in React? Or maybe there is a bug?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪眸﹌ 2025-01-23 08:45:50

事实证明,我只需要在 Stack Overflow 上发帖就可以最终解决这个问题。我有一个 if 语句

        {itemsList.map((item) => {
          if ((item.name = "variable")) {

,它既充当 if 语句又充当赋值运算符,因为我忘记放入第二组 = 。

As it turns out, I just needed to post on Stack Overflow to finally solve the issue. I have an if statement

        {itemsList.map((item) => {
          if ((item.name = "variable")) {

And it was functioning as both an if statement and an assignment operator because I forgot to put in the second set of = .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文