嗨,我一直在尝试做以下反应,但我收到了这个错误:
未捕获的错误:对象作为 React 子对象无效(发现:对象与键 {id, address, long, lat, cityId, cityDistrict, phone, name, userId, city})。如果您打算渲染一组子对象,请改用数组或使用 React 附加组件中的 createFragment(object) 包装对象。检查Comparison
.
这是我的代码:
class Comparison extends Component {
render() {
let comparedProperties = {
id: [1001, 1002],
address: ["abc", "def"]
};
let comparedItemsData = [];
for (var key in comparedProperties) {
if (comparedProperties.hasOwnProperty(key)) {
let newTR = <tr key={Math.random()} className="compare-table-row">
<td className="table-item-header">
{key}
</td>
{comparedProperties[key].map((item) => {
return <td key={Math.random()} className="table-item">{item}</td>;
})}
</tr>;
comparedItemsData.push(newTR)
}
}
return (
<table className="compare-table">
<tbody>
{comparedItemsData}
</tbody>
</table>
)
}
}
const mapStateToProps = (state) => ({
...state
});
const mapDispatchToProps = (dispatch) => ({
actions: bindActionCreators(Actions, dispatch)
});
export default connect(
mapStateToProps,
mapDispatchToProps
)(Comparison);
如何在这个重复的 tr 标签中重复我的 td 标签?并且仅在我的 compareProperties 对象的这一循环中
更新答案:
所以我发现问题出在哪里,但是我从反应中得到了更好的错误消息,问题是在我的 compareProperties 中,我在数组中有一个导致错误的对象
let comparedProperties = {"id":[101,102],"estateAgency":[{"id":1},{"id":2}]}