reactjs / reactjs/react.dev

improve the phrasing for using "no keys" on https://reactjs.org/docs/lists-and-keys.html#keys

Đang mở
#3,732 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

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ả

https://reactjs.org/docs/lists-and-keys.html#keys currently has a section that causes confusion about React's behaviour when omitting key attributes, currently reading:

"We don’t recommend using indexes for keys if the order of items may change. This can negatively impact performance and may cause issues with component state. Check out Robin Pokorny’s article for an in-depth explanation on the negative impacts of using an index as a key. If you choose not to assign an explicit key to list items then React will default to using indexes as keys."

The emphasized sentence has led to folks thinking that React knows what a specific map()'s iteration index is, and that it'll use that, rather than that the index talked about is the child index of the element in the vdom. To remove that confusion and make it explicit that React is using an internal index, I would recommend changing the phrasing here to:

"We don’t recommend using indexes for keys if the order of items may change. This can negatively impact performance and may cause issues with component state. Check out Robin Pokorny’s article for an in-depth explanation on the negative impacts of using an index as a key. If you choose not to assign an explicit key to list items then React will default to using element index in the virtual DOM tree as keys."

It might be even more high value to also add one or more sentences to explain that this means that the behaviour for consecutive maps that don't use keys vs. incorrectly use iteration indices as keys will lead to different behaviour, which folks right now mistakenly assume would behave the same.

E.g.

render() {
  const { text, additional } = this.state;
  const content = text.map(t => <p>{t}</p>);
  const addenda = additional.map(t => <p className="add">{t}</p>);
  return <section>{content}{addenda}</section>;
}

will work, but:

render() {
  const { text, additional } = this.state;
  const content = text.map((t,i) => <p>{t}</p>);
  const addenda = additional.map((t,i) => <p className="add" key={i}>{t}</p>);
  return <section>{content}{addenda}</section>;
}

will not, and if you are familiar enough with React already to not need the docs, you know why, but if you're not and you read the docs, it is entirely understandable if you come away convinced that this should work after reading the current text.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với phần Keys tại https://reactjs.org/docs/lists-and-keys.html#keys và xem lại cách diễn đạt về các Keys bị bỏ qua cùng với những ví dụ được cung cấp. Làm rõ rằng React sử dụng chỉ mục của phần tử trong virtual DOM tree, và quyết định xem có nên thêm cả ví dụ về các map liên tiếp hay không; tài liệu phải phân biệt chính xác cả hai hành vi.

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ó
2/5
Thời gian dự kiến
1-3 giờ
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
48/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.