reactjs / reactjs/react.dev

It is a myth and misunderstanding the key={...} make the display correct

Offen
#6,292 0 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
JavaScript
Sterne
11.8k
Forks
7.9k
Ø Merge
1 T. 11 Std.
Gemergte PRs (30 T.)
11

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, die Rendering Lists-Seite unter https://react.dev/learn/rendering-lists zu lesen, einschließlich der zitierten Abschnitte über Schlüssel und Array-Indizes. Überprüfe die Aussagen anhand des dokumentierten Verhaltens von React und ermittle, ob der Wortlaut einer Klarstellung bedarf; als erledigt gilt die Aufgabe, wenn eine abgestimmte, technisch korrekte Erklärung auf dieser Seite wiedergegeben ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, react
Bereich
documentation
Issue-Typ
Dokumentation
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.