简体   繁体   English

警告:列表中的每个孩子都应该有一个唯一的“关键”道具。 检查 `RenderComments` 的渲染方法

[英]Warning: Each child in a list should have a unique "key" prop. Check the render method of `RenderComments`

I know I need a unique key, but where do I add it?我知道我需要一个唯一的密钥,但我应该在哪里添加它? This is the JSON file:这是 JSON 文件:

"comments": [
    {
      "dishId": 1,
      "rating": "4",
      "author": "Customer1",
      "comment": "This is a test Comment",
      "date": "2022-02-04T12:34:15.994Z",
      "id": 21
    }
]

The RenderComment's render method is as follows: 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>,
                                            &nbsp;
                                            {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>
        );
}

Edit: I have tried changing "comments._id" to "comments.id" but its not helping.编辑:我尝试将“comments._id”更改为“comments.id”,但没有帮助。

I see that you use _id prop in map, but in json we have id prop.我看到你在 map 中使用了_id属性,但在 json 中我们有id属性。 Try to change key={comment._id} to key={comment.id} .尝试将key={comment._id}更改为key={comment.id} Because if the objects doesn't have _id prop, your key always will be undefined因为如果对象没有_id道具,您的密钥将始终undefined

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:数组或迭代器中的每个子代均应具有唯一的“键”道具。 检查`单位`的渲染方法 - Warning: Each child in an array or iterator should have a unique “key” prop. Check the render method of `Units` 警告:数组或迭代器中的每个子代均应具有唯一的“键”道具。 检查“搜索”的渲染方法 - Warning: Each child in an array or iterator should have a unique “key” prop. Check the render method of 'search' 列表中的每个孩子都应该有一个唯一的“key”道具。 检查`Form`的渲染方法 - Each child in a list should have a unique “key” prop. Check the render method of `Form` 收到警告“警告:列表中的每个孩子都应该有一个唯一的”key“道具。” 当我的组件渲染时 - Getting warning “Warning: Each child in a list should have a unique ”key“ prop.” when my component render 警告:列表中的每个孩子都应该有一个唯一的“key”道具。 即使已经设置了密钥 - Warning: Each child in a list should have a unique "key" prop. Even after setting the key already 正在渲染对象数组的数组,不断得到“警告:列表中的每个孩子都应该有一个唯一的“键”道具。 - Was rendering arrays of arrays of objects, keep getting “Warning: Each child in a list should have a unique ”key“ prop.” 警告:列表中的每个孩子都应该有一个唯一的“关键”道具。 mapStateToProps - Warning: Each child in a list should have a unique "key" prop. mapStateToProps 警告:列表中的每个孩子都应该有一个唯一的“key”道具。 - ReactJS - Warning: Each child in a list should have a unique "key" prop. - ReactJS 警告:列表中的每个孩子都应该有一个唯一的“关键”道具。 如何解决这个问题? - Warning: Each child in a list should have a unique "key" prop. how to fix this? 警告:数组或迭代器中的每个子代都应具有唯一的“键”道具。 li中已添加的关键道具 - Warning: Each child in an array or iterator should have a unique “key” prop.; key prop already added in li
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM