Skip to content

QuizNot attempted

Spot the bug: uuid keys in render

What happens to these rows every time the table re-renders (a page loads, a header is clicked)?
{sortedData.map((row, index) => {
const rowId = uuidv4();
return (
<tr key={rowId} ref={index === sortedData.length - 1 ? lastElementRef : null}>
{columns.map((column) => (
<td key={`${rowId}-${column.key}`}>{row[column.key]}</td>
))}
</tr>
);
})}
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • Every row unmounts and remounts, because no key matches the previous render
  • Keys must be unique AND stable across renders.
  • A fresh uuid per render never matches last render's key, so React destroys every <tr> and builds new DOM: slow for long lists, and focus, selection and any row state are lost.
  • Use the record's id (key={row.id})
  • if the data has none, assign ids once when it arrives, never during render.
  • Not unique is enough: uniqueness without stability is exactly what forces the remount.
  • Not reuses by position: a key that doesn't match is a new element
  • position only matters with no keys or index keys.
  • Not duplicate-key warning: uuids never collide, so React stays silent, which is why this survives review.