It is a myth and misunderstanding the key={...} make the display correct
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- JavaScript
- Star
- 11.8k
- Fork
- 7.9k
- Merge trung bình
- 1 ngày 11 giờ
- Pull request đã merge (30 ngày)
- 11
Mô tả
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.
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách đọc trang Rendering Lists tại https://react.dev/learn/rendering-lists, bao gồm các phần được trích dẫn về key và chỉ mục mảng. Đối chiếu các nhận định với hành vi được tài liệu hóa của React và xác định xem cách diễn đạt có cần được làm rõ hay không; hoàn thành nghĩa là một phần giải thích được thống nhất và chính xác về mặt kỹ thuật được phản ánh trên trang đó.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- javascript, react
- Lĩnh vực
- documentation
- Loại issue
- Tài liệu
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100