【发布时间】:2019-08-08 12:26:36
【问题描述】:
我正在尝试在 React 中构建一个可重用的表格组件,但在渲染我的 <td>s 方面到目前为止还没有运气。
我将我需要的道具从TableList 组件传递给我的TableRow 组件,如下所示:
const TableList = props => {
const { listData, listHeaders } = props;
return (
<table className="table table-striped">
<thead>
<tr>
{listHeaders.map(header => (
<th scrope="col" key={header}>
{header}
</th>
))}
<th scope="col" />
</tr>
</thead>
<tbody>
{listData.map(data => (
<TableRow
key={data.id}
data={data}
handleClick={props.handleClick}
/>
))}
</tbody>
</table>
);
};
在TableRow 中,我可以很好地迭代我的 JSON 对象,但我的元素没有呈现。我没有收到任何错误,但我在 DOM 中的 <tr> 元素中没有子元素。这是我的TableRows 组件:
const TableRow = props => {
const { data } = props;
return (
<tr>
{Object.keys(data).forEach(function(item) {
console.log(data[item]); // This prints out my JSON objects just fine
<td key={item}>data[item]</td>; // But this element isn't rendering in the DOM
})}
</tr>
);
};
我尝试将我的 JSON 对象分配给 .map() 导航的数组,但也得到了相同的结果。此外,我尝试在组件树上上下移动各种元素,如 <tbody> 和 <tr>,但没有成功。
【问题讨论】:
标签: javascript arrays reactjs