【问题标题】:React typeError, useState doesnt work. but I dont know why反应 typeError,useState 不起作用。但我不知道为什么
【发布时间】:2021-08-27 21:21:51
【问题描述】:

发生类型错误。当我运行我的代码时。 你可以看到我试图检查 (res.data) 是否有效。 但我使用 useState 和 setUser(res.data) 但 User 值为空。 我不知道为什么。

【问题讨论】:

  • 请复制代码,而不是图像。图片是显示代码或错误的一种非常糟糕的方式

标签: html reactjs react-hooks


【解决方案1】:

User 开始null。这就是它在setUser 之前的价值。 错误发生在User.profilePicture ? User.profilePicture : ...,因为您试图从User 访问一个属性,即null

要解决它,先检查值是否存在,然后检查适当性是否存在。

User && User.profilePicture ? User.profilePicture : ...

【讨论】:

  • 感谢您的提及!我解决了这个错误。
猜你喜欢
  • 2021-10-26
  • 2020-12-15
  • 1970-01-01
  • 2017-04-28
  • 1970-01-01
  • 2016-08-25
  • 2013-11-27
  • 1970-01-01
  • 2019-11-12
相关资源
最近更新 更多