【发布时间】:2020-12-02 11:20:58
【问题描述】:
我有一个从 API 读取数据的功能组件。我已经定义了一个接口,但无法使用 Map in react 在表格中循环和显示。
错误
index.js:1 Warning: Each child in a list should have a unique "key" prop.
界面
export interface ISiteManager{
managerId: number,
name: string,
isDeleted: false
}
React 函数式组件返回模板
...
return (
<div>
<h2>EziTracker Dashboard Report</h2>
{eziStatusCollection && eziStatusCollection.length >0 && (
<table className="table">
<thead>
<tr>
<th>ManagerId</th>
<th>Name</th>
<th>Is Deleted</th>
</tr>
</thead>
{
eziStatusCollection.map((item, index) => {
return(
<tbody>
<tr key={index}>
<td>{item.managerId}</td>
<td>{item.name}</td>
<td>{item.isDeleted}</td>
</tr>
</tbody>
)})
}
</table>)}
</div>
);
};
export default MyComponent;
【问题讨论】: