reactjs / reactjs/react.dev

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

オープン
#6,292 コメント 0 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
JavaScript
スター
11.8k
フォーク
7.9k
平均マージ
1日 11時間
マージ済み PR(30日)
11

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず https://react.dev/learn/rendering-lists の Rendering Lists ページを読み、key と配列インデックスについて引用されているセクションも確認します。主張を React の文書化された動作と照合し、文言に明確化が必要かどうかを判断します。完了とは、合意された技術的に正確な説明がそのページに反映されていることを意味します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, react
領域
documentation
issue の種類
ドキュメント
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。