React | Lists and Keys | Question 6

Last Updated :
Discuss
Comments

What is the issue with this code in terms of key assignment?

JavaScript
const list = ["apple", "banana", "cherry"];
return (
    <ul>
        {list.map((fruit, index) => (
            <li key={index}>{fruit}</li>
        ))}
    </ul>
);


Using index as a key is fine in all cases

index should not be used as a key when the list can change dynamically

The key should always be a string

key should be assigned to the ul tag

Share your thoughts in the comments