<分区>
在我的 React 组件中,我想使用 map 函数来显示数组中的一些消息。数组非常简单,只有消息:
const messages = [
"John called and left a message.",
"Marketing report is ready!",
"Today's sales meeting is cancelled."
]
我不想再让这个数组复杂化,所以这些消息没有 ID。
当我尝试在我的 React 组件中使用 map 函数时,它希望我为每个项目设置一个唯一的键/ID。如何使用此数组中每个项目的索引?
我试过这段代码
<ul>
{messages.map(item => {
<li key={item.index}>{item}</li>
})}
</ul>
但收到一条错误消息:
Each child in an array or iterator should have a unique "key" prop.