我是 React 新手,我正在关注 React 类(class)。下面的代码出现了类似“TypeError: Cannot read property 'map' of undefined”的问题;谁能告诉发生了什么事吗?非常感谢!
import React, { Component } from 'react'
const ListGroup = props => {
const {items,textProperty,valueProperty} = props;
return (
<ul className="list-group">
{items.map(item =>(
<li key={item[valueProperty]} className="list-group-item">
{item[textProperty]}
</li>
))}
</ul>
);
}
export default ListGroup;
最佳答案
您似乎没有将 items
属性传递给 ListGroup
:
<ListGroup items=[] />
或者,您可以为项目分配默认值:
const {items = [],textProperty,valueProperty} = props;
或者使用 elvis 运算符:
items?.map(item => {})
关于reactjs - 类型错误 : Cannot read property 'map' of undefined"React,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66761333/