React hook 卡在先前的对象值上
我有一个正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,我只需要在 Stack Overflow 上发帖就可以最终解决这个问题。我有一个 if 语句
,它既充当 if 语句又充当赋值运算符,因为我忘记放入第二组 = 。
As it turns out, I just needed to post on Stack Overflow to finally solve the issue. I have an if statement
And it was functioning as both an if statement and an assignment operator because I forgot to put in the second set of = .