【发布时间】:2019-12-03 00:25:31
【问题描述】:
我有一张产品表
所以我在这里有一列 product_images 包含 id 数组(用作图像的来源)。 使用道具我正在映射这些产品,如下所示:
render() {
return (
<>
<ul className="row">
{this.props.products
.map(product => <li className="card col-md-4" key={product.id}>
<h2>{product.name}</h2>
<p>{product.description}</p>
<p>{product.sub_categories}</p>
<b>{product.created}</b>
{Object.keys(product.product_images)
.filter(v => product.product_images[v] != null)
.map(product_image =>
<div key={product.product_images}>
<img height='100%' alt='hello'
src={"IMAGE_PATH" + product.product_images[product_image]} />
</div>
)}
</li>)}
</ul>
</>
);
}
这给了我每个产品。我还想得到的是数组中的每个 id 以在产品中显示多个图像。 我知道我在映射对象时做错了。 作为 javascript 和 react 的初学者,如果你们能帮助我,那就太好了。
我也愿意接受其他有趣的解决方案。谢谢
【问题讨论】:
-
这里到底有什么问题?它工作正常吗?
-
使用地图时,您可以将
index参数传递给map,例如map((product_image, index) => //etc) -
我现在收到此错误:警告:遇到两个孩子使用相同的密钥,
1575290166391,1575290166394。密钥应该是唯一的,以便组件在更新时保持其身份。非唯一键可能会导致子项被重复和/或省略——这种行为不受支持,并且可能在未来的版本中发生变化。
标签: javascript mysql arrays reactjs mapping