0

I am not sure how to solve this warning message??

"Warning: Each child in an array or iterator should have a unique "key" prop".

  let theComments = 
  this.props.state.studentCommentReducer.studentCommentReducer.map((comments) => {
  return <CommentsItem key={comments.id} comments={comments} />;
  });



     Student: {this.props.comments.comment}
Just_some_Noob
  • 139
  • 1
  • 1
  • 12

3 Answers3

2

In some way you need to specify a key for each item. You can do something like this

...map((comments) => {
  return <CommentsItem key={comments.id} comments={comments} />;
});

or

...map((comments, i) => {
  return <CommentsItem key={i} comments={comments} />;
});

Ref.: https://reactjs.org/docs/lists-and-keys.html#keys-must-only-be-unique-among-siblings

1

You need to specify an unique key for each of the mapped CommentsItem, preferably not the index of the array.

In your code, make sure that comments.id is unique. That would solve the warning message you are receiving.

palsrealm
  • 5,083
  • 1
  • 20
  • 25
0

You can use index as the key only to the lists which are static .try using others as key