It is a myth and misunderstanding the key={...} make the display correct
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 11.8k
- Forks
- 7.9k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 11
Description
On page https://react.dev/learn/rendering-lists
it is said that:
A well-chosen key helps React infer what exactly has happened, and make the correct updates to the DOM tree.
That's a myth that the key keeps the updates correct. It helps to make the update faster, so that it goes by row level diff'ing, instead of cell level diff'ing.
For example, if you have a table with 5 fields. They key can make it so that the diff'ing happens with the prev row, so that it is faster.
Imagine if row 23 supposedly needed to compare with row 24, due to row added or deleted, but due to the key being bad, it is compared to row 23, and all the fields are different, so React needs to update it cell by cell.
If the key is good, however, now row 23 will be compared with row 24, and nothing changes, and no update is needed whatsoever.
You can check with an advanced engineer about this. The myth about "you need a good key so that the displayed data is correct", is wrong.
Maybe the original line "make the correct updates to the DOM tree" doesn't suggest that, but it can make readers think this way. So you may want to rephrase it or further explain it.
So this part later in the page:
You might be tempted to use an item’s index in the array as its key. In fact, that’s what React will use if you don’t specify a key at all. But the order in which you render items will change over time if an item is inserted, deleted, or if the array gets reordered. Index as a key often leads to subtle and confusing bugs.
is also a wrong concept. It will NOT lead to subtle and confusing bugs. It will just make it slow, but the display will still all be correct.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the Rendering Lists page at https://react.dev/learn/rendering-lists, including the quoted sections about keys and array indexes. Verify the claims against React's documented behavior and determine whether the wording needs clarification; done means an agreed, technically accurate explanation is reflected on that page.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, react
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100