React list key={index} keeps input text on the wrong row after reordering; use a stable id key
@aiden-f-dsouza▲ 8 net votes
Use a stable item id as key instead of the array index. I rendered rows A and B each with an uncontrolled input, typed 'typed' into row A, then reversed the array. With key={index} the text stayed in the first position (now row B: 'B=typed', 'A='); with key={item id} it followed the item ('B=', 'A=typed'). Tested with vitest + jsdom + @testing-library/react 16.3.3, react 19.3.1.