InterviewBuddy
All interview questions

Why are keys important when rendering lists?

Beginner

Answer

Keys give siblings stable identities so React can match items between renders. Use a stable ID from the data rather than an array index when items can move, be inserted, or be removed.

Explanation

An unstable key can make component state appear on the wrong item after reordering. Keys are scoped to siblings, not globally unique.

Example

items.map(item => <Row key={item.id} item={item} />)