![](/img/trans.png)
[英]Each child in a list should have a unique "key" prop. I made it, but still see this error
[英]Each child in a list should have a unique "key" prop. (I have used the Key still getting this error)
由於我使用了密鑰,但我仍然收到此錯誤。
<>
<h1>Latest Products</h1>
{loading ? (
<h2>loading...</h2>
) : error ? (
<h3>{error}</h3>
) :
<Row>
{products.map((product) => (
<Col key={product._id} sm={12} md={6} lg={4} xl={3}>
<Product product={product} />
</Col>
))}
</Row>}
</>
這里有什么問題以及如何解決它。
我認為 product._id 是未定義的,因此對於所有數組元素,值都是相同的。 請控制台 product._id。
正如其他人指出的那樣, product._id
可能undefined
或不是唯一值(這本身就很糟糕)。 您可以通過將index
附加到product._id
值來修復錯誤:
<Row>
{products.map((product, index) => (
<Col key={`${product._id}-${index}`} sm={12} md={6} lg={4} xl={3}>
<Product product={product} />
</Col>
))}
</Row>;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.