U
U
uRoot2020-12-13 20:12:24
React
uRoot, 2020-12-13 20:12:24

Why does React complain about key if it exists and is unique?

There is a component:

import Comment from "../Comment/Comment";

export default function Comments({ comments }) {
  return(
    <div className={"comments"}>
      {comments.map(element =>
        <Comment
          forKey={element.id}
          name={element.name}
        />
      )}
    </div>
  )
}

export default function Comment({ forKey, name }) {
  console.log(forKey)
  return(
    <div key={forKey} className={"comment"}>
      {name}
    </div>
  )
}


I end up with an error: Warning: Each child in a list should have a unique "key" prop.
Although the keys are unique. Proof:
5fd64b2593f31793271214.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Stolyarov, 2020-12-13
@uroot

The key must be where the array is directly formed, that is, inside the map

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question