Using useId to make list keys in React
List rows lose their state or typed text when the list updatesShort 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.
Fix it fast
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/2Match what you see, then jump to its fix
- 1
A call to useId feeds the key of list items.useId is used to produce keysFix - 2
key={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
key={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
- 1
What does the error look like?
List rows lose their state or typed text when the list updatesKeys 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 devConfirmed by react.dev
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 devConfirmed by react.dev
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 devConfirmed by react.dev
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fix