← BYTES
React.js·Intermediate·Ref: 4.2.8
When rendering a list of items with `.map()`, why is using the array index as the `key` prop often discouraged?
Have a question about this video? Mention reference 4.2.8 when you contact us.
a.If the list can be reordered, filtered, or have items inserted/removed, index-based keys can cause React to mismatch state and DOM nodes to the wrong items after the reorder
b.Indexes are not valid JavaScript values for a `key` prop
c.Using index as key always causes an infinite render loop
d.It is discouraged purely for style reasons, with no real functional consequence
Keys tell React which array entry maps to which rendered element across renders. If the list's order or contents change, an index-based key can end up pointing at a different logical item than before, causing component state or focus to "stick" to the wrong row -- a stable, item-specific key (like a database id) avoids this.
Find this on YouTube & Instagram
Every BYTES video is tagged with its reference number and topic — search any of these on YouTube or Instagram to find this one:
#BYTES#BYTES428#Reactjs