reactjs - 对于单个产品:React TypeError: Cannot read property 'map' of undefined :
问题描述
这是单品数据。
const [single, setSingle]= useState([])
setSingle(res.data) (axios set state)
{
"id": 135,
"device_detail": [
{
"type": "high-speed",
"brief": "dsf",
"model": "ds",
"serial": "ds"
},
{
"type": "low-speed",
"brief": "dfg",
"model": "hgj",
"serial": "dsf"
}
],
"device_name": "Handpieces",
"practice_name": "dsf",
"unit": "dsf",
"street": "dsf",,
"status": 1,
"created_at": "2021-03-02 14:00:58.295738+00:00"
}
当我使用
{single.device_detail.map((subd,index)=>(
(index ? ', ' : '') + subd.type
))}
它说 TypeError: Cannot read property 'map' of undefined :
但是当我为 device_detail 创建另一个状态,然后它的工作方式如下:
const [dd, setdd]= useState([])
setdd(res.data.device_detail) (axios set state)
但是,我这不是一个合适的解决方案。
任何想法?
解决方案
推荐阅读
- c++ - C++ 模板模糊实例化
- reactjs - React-scroll:导航链接不会滚动到同一页面的不同部分
- java - 客户端中的Java接口实现
- python - 当我想定义一个数组时,列表分配索引超出范围
- python - Django 3 中的 Python 2 脚本
- python - 使用集成 Python-Behave-Selenium 在 Jenkins 中出错
- maven - 更改 maven 本地存储库的 URL 后,maven 停止工作
- python - 不能再将 import pandas 作为 pd 导入
- python - 日期时间解析问题导致此错误:{ValueError: invalid literal for int() with base 10: '`' }
- algorithm - Best Conceivable Runtime 与 Best Case Runtime 之间的差异