Index as React key
Index as React key. Uses the list position (or a random value) as the React key.
This is how the comment reads inline, under the flagged line.
Why it matters
When items move or are removed, index keys make React reuse the wrong component state; random keys remount every render.
What to do
Use a stable id from the data.
Examples
<Row key={index} item={item} /><Row key={item.id} item={item} />What it catches
It reads a pattern on each line, aware of strings and comments. Languages: TypeScript and JavaScript. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.
Flagged
<Row key={index} item={item} />Left alone
<Row key={item.id} item={item} />Research
Array index, time or random values as React keys. Reordering remounts items or attaches state to the wrong one.
- 14 patterns AI code generators get wrong
- owner's notes
Turn it off or change it
In a config file for the CLI, or in the API's rules field:
{
"rules": {
"disable": [
"index-as-key"
],
"severity": {
"index-as-key": "low"
}
}
}