【发布时间】:2018-10-27 09:09:36
【问题描述】:
我目前正在尝试在 React 中制作 HTML 表格,但有些事情并没有按照应有的方式进行。我确实得到了渲染输出,但错误为:<tr> cannot appear as a child of <div>。
我试图在网上找到它,但我使用的技术似乎并不经常使用,所以如果我做错了,请赐教。
提前谢谢!
getTableContent = (arr) => {
let result = [];
arr.forEach(function (item, i) {
result.push(
<table key={item.productType}>
<thead>{item.productType}</thead>
<tbody>
{item.contents.forEach(function (nextItem, j) {
result.push(
<tr key={nextItem.type}>
<td>{nextItem.type}</td>
<td>{nextItem.count}</td>
</tr>
)
})}
</tbody>
</table>
);
});
return result;
};
render() {
return (
<div>{this.getTableContent(productSpecification)}</div>
);
}
数据如下:
const productSpecification = [
{
productType: "abc", contents: [
{type: "abc", count: 231},
{type: "abc", count: 56},
{type: "abc", count: 54},
{type: "abc", count: 544},
{type: "abc", count: 54},
{type: "abc", count: 564},
{type: "abc", count: 4},
{type: "abc", count: 4564},
{type: "abc", count: 4531},
{type: "abc", count: 234},
{type: "abc", count: 57},
{type: "abc", count: 7}
]
}
];
【问题讨论】:
-
这可能不是您的问题的原因,但是您的表格行不会都具有相同的
key吗?您可以使用j作为唯一键(也许您的意思是这就是j存在的原因?)。 -
我没有收到密钥重复的错误,但仍然感谢您的输入!
标签: reactjs html-table