![](/img/trans.png)
[英]Warning: Each child in an array or iterator should have a unique “key” prop. Check the render method of `Units`
[英]Warning: Each child in a list should have a unique "key" prop. Check the render method of `RenderComments`
我知道我需要一個唯一的密鑰,但我應該在哪里添加它? 這是 JSON 文件:
"comments": [
{
"dishId": 1,
"rating": "4",
"author": "Customer1",
"comment": "This is a test Comment",
"date": "2022-02-04T12:34:15.994Z",
"id": 21
}
]
RenderComment 的渲染方法如下:
function RenderComments({ comments, postComment, dishId }) {
if (comments != null)
return (
<div className="col-12 col-md-5 m-1">
<h4>Comments</h4>
<ul className="list-unstyled">
<Stagger in>
{comments.map((comment) => {
return (
<Fade in key={comment._id}>
<li>
<p>{comment.comment}</p>
<p><b>-{comment.author}</b>,
{new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: 'long',
day: '2-digit'
}).format(new Date(comment.date))}
</p>
</li>
</Fade>
);
})}
</Stagger>
</ul>
<CommentForm dishId={dishId} postComment={postComment} />
</div>
);
else
return (
<div></div>
);
}
編輯:我嘗試將“comments._id”更改為“comments.id”,但沒有幫助。
我看到你在 map 中使用了_id
屬性,但在 json 中我們有id
屬性。 嘗試將key={comment._id}
更改為key={comment.id}
。 因為如果對象沒有_id
道具,您的密鑰將始終undefined
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.