react-stable-map-keys
Replace React .map() index-as-key with stable keys (e.g. item.id) when verifiable; add TODO when not.
Problem
Using the array index as the key prop in React lists causes:
- Reconciliation bugs: Reordering, inserting, or removing items can cause wrong component state to persist
- Performance: React may re-render more than necessary
- Accessibility: Screen readers can get confused by inconsistent identity
Solution
The codemod finds items.map((item, index) => <Component key={index} ... />) patterns and:
-
Replaces with
key={item.id}when we can confidently tellidexists:- The callback body already uses
item.id(or the first param name) - The array is a literal like
[{id: 1, name: "a"}, ...]where each element hasid
- The callback body already uses
-
Adds TODO when we cannot verify: a comment above the
.map()call for manual fix.
Usage
bash
Or run the transform directly:
bash
Params
| Param | Type | Default | Description |
|---|---|---|---|
| create_branch | boolean | true | Create git branch and commit after each step |
| run_ai_step | boolean | false | Run AI step for TODO-flagged cases |
| publish_pr | boolean | false | Create PR after push |
| main_branch | string | main | Target branch for PR |
Metrics
- react-stable-map-keys:
outcome: replaced— key was replaced with stableitem.idoutcome: todo-added— TODO comment added, manual fix neededconfidence: body-uses-id|array-literal-has-id|unknown
What Gets Fixed
Automated (replaced)
users.map((user, i) => <div key={i}>{user.id} - {user.name}</div>)→key={user.id}[{id: 1, n: "a"}].map((item, idx) => <li key={idx}>{item.n}</li>)→key={item.id}
Flagged (TODO)
items.map((item, i) => <div key={i}>{item.name}</div>)whenidcannot be verified
License
MIT