Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Using useId to make list keys in React

ErrorList rows lose their state or typed text when the list updates

Short answer

The React docs say not to call useId to generate keys in a list: keys should come from your data. Use a stable id stored with each item, such as a database id, or a value from crypto.randomUUID() created once when the item is made.

  • Applies to React 18, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find where the key is produced. A key built during render, from useId, Math.random or crypto.randomUUID, is the problem.

    grep -rn "key={" src/
  2. 2Match what you see, then jump to its fix

    1. 1A call to useId feeds the key of list items.useId is used to produce keysFix
    2. 2key={Math.random()} or a fresh uuid inside the map, and rows lose their input when the list updates.Keys are generated on every renderFix
    3. 3key={index}, and rows show the wrong state after insert, delete or reorder.The array index is used as the key in a list that changesFix

What does the error look like?

  • List rows lose their state or typed text when the list updates
  • Keys built from useId inside a list's map callback

Cause 1 of 3

useId is used to produce keys

1Check

The useId reference says it should not be used to generate keys in a list and that keys should be generated from your data.

{items.map((item) => {
  const id = useId(); // not allowed here
  return <li key={id}>{item.name}</li>;
})}

2Fix

Use a stable id that is part of each item. Keys should be generated from your data.

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

3Verify

Reload; the key warning should be gone and typed input inside rows should survive an update.

npm run dev

Confirmed by react.dev

Did this fix it?

Cause 2 of 3

Keys are generated on every render

1Check

The docs say a key must not change, and generating it while rendering defeats its purpose. A random key never matches between renders, so the whole list is recreated.

grep -rn "key={Math.random\|key={crypto" src/

2Fix

Store the id when the item is created, either the database id or a value from crypto.randomUUID() assigned once.

function addItem(name) {
  setItems((prev) => [...prev, { id: crypto.randomUUID(), name }]);
}

3Verify

Type into an input inside a row, then trigger an update to the list; the typed text should stay.

npm run dev

Confirmed by react.dev

Did this fix it?

Cause 3 of 3

The array index is used as the key in a list that changes

1Check

The docs say index keys often lead to subtle bugs when items are inserted, deleted or reordered, because the order changes over time.

grep -rn "key={index\|key={i}" src/

2Fix

Use the item's own id. An index key is only reasonable for a list that never changes order, such as fixed lines of text.

{people.map((person) => (
  <li key={person.id}>{person.name}</li>
))}

3Verify

Delete the first row and check that the remaining rows keep their own state.

npm run dev

Confirmed by react.dev

Did this fix it?

How do I stop it coming back?

  • Add an id to every record at the point it enters your app, from the database or at creation.
  • Keep keys in the data, not in the render code.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.