在react时使用map遍历数组报错index.jsx:8 Uncaught TypeError: Cannot read properties of undefined (reading ‘map’)
这是因为第一次数组里面是空数组,没有值,所以需要对其进行遍历
错误代码:
<ul className="todo-main">
{
todos.map((todoObj)=>{
return <Items key={todoObj.id} {...todoObj}/>
})
}
</ul>
正确代码:
<ul className="todo-main">
{
todos && todos.map((todoObj)=>{
return <Items key={todoObj.id} {...todoObj}/>
})
}
</ul>