2017-10-07 83 views
0

我共同面临的Warning: Each child in an array or iterator should have a unique "key" prop错误与我的道具组成部分,尽管设置了关键我阵列的unqiueID为返回的div元素我看到这个警告的关键。我是否将它设置在错误的元素上?它可能与嵌套map有关吗?ReactJS - 缺少关键尽管在元

JSON实例:

{ 
annotationId: 117, 
title: "Test", 
discovery: "Test 123", 
annotation_comments: [{ 
annotationCommentId: 12, 
comment: "Lorem Ipsum" 
}] 
} 

详细的错误:

in AnnotationCard (created by AnnotationFeed) 
    in AnnotationFeed (created by AnnotationFeedContainer) 
    in div (created by AnnotationFeedContainer) 
    in AnnotationFeedContainer (created by Annotation) 
    in div (created by Annotation) 
    in Annotation 

组件:

//Loop through JSON and component 
const AnnotationFeed = props => { 
    return (
     <div> 
     { 
      props.annotations.map((annotation, index) => { 
       return (
        <AnnotationCard {...annotation}> 
         { annotation.annotation_comments.map((comment, i) => <Comments {...comment} />)} 
        </AnnotationCard> 
       ); 
      }) 
     } 
     </div> 
    ) 
} 

const AnnotationCard = props => { 
    return (
     <div key={props.annotationIdHash}> 
      <h4>{props.title}</h4> 
      <p>{props.discovery}</p> 
     </div> 
    ) 
} 

const Comments = props => { 
    return (
     <div key={props.annotationCommentId}> 
      <h4>{props.comment}</h4> 
     </div> 
    ) 
} 
+0

你没有设定一个关键道具 – linasmnew

回答

2

您需要的关键这里

<AnnotationCard {...annotation} key={index}>

这里

<Comments {...comment} key={i}/>

0

这个错误清楚地说,AnnotationCardComments组件需要一个关键属性。所以你应该为这两个组件设置一个关键属性,而不是里面的div标签。