Skip to content

How to Fix React Key Prop Errors

DodaTech Updated 2026-06-26 1 min read

The Problem

In this quick-fix, you'll learn how to use key prop correctly to avoid re-render bugs. This matters because incorrect keys cause React to mismanage component state. In real-world applications, this pattern appears in todo lists product catalogs chat message lists.

The Wrong Way

function TodoList({ todos }) {
  return <ul>{todos.map((t, i) => <li key={i}><input defaultValue={t.text} /></li>)}</ul>;
}
After delete first item second input retains first items text

The Right Way

function TodoList({ todos }) {
  return <ul>{todos.map(t => <li key={t.id}><input defaultValue={t.text} /></li>)}</ul>;
}
After deletion remaining items keep correct state

Prevention

  • Use stable unique IDs as keys never array index
  • Generate IDs with uuid or nanoid
  • For static lists index keys are acceptable

FAQ

### Why does index as key cause bugs??

When list changes React matches elements by keys. With index keys deleting item 0 reuses DOM for item 1 with its local state.

Can I use item value as key for primitives??

Yes for unique stable strings/numbers. items.map(item =>

  • {item}
  • ).

    How does React use keys internally??

    React uses keys during reconciliation to identify changes. Stable keys let React reuse DOM nodes efficiently.

    This guide is brought to you by DodaTech — built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

    Built by the developers of DodaTech

    Doda Browser, DodaZIP & Durga Antivirus Pro