reactjs / reactjs/react.dev

Amend documentation for skipping useEffect

Đang mở
#1,799 2 bình luận 1 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ả

This is a documentation improvement request.

Currently https://reactjs.org/docs/hooks-effect.html#tip-optimizing-performance-by-skipping-effects explains skipping a useEffect based on a state variable by showing the following code:

useEffect(() => {
  document.title = `You clicked ${count} times`;
}, [count]); // Only re-run the effect if count changes

With the following explanation:

In the example above, we pass [count] as the second argument. What does this mean? If the count is 5, and then our component re-renders with count still equal to 5, React will compare [5] from the previous render and [5] from the next render. Because all items in the array are the same (5 === 5), React would skip the effect. That’s our optimization.

However, as the array we're shown only has a single element it, it is ambiguous what "all the items in the array" means. E.g. we cannot tell whether that's intentional phrasing, or just "plain English" that isn't actually tied to any technical precision.

Could this phrasing either be made more explicit, or (ideally) can an extra paragraph be added to explain what happens to an array with more than one element?

(originally filed as from https://github.com/facebook/react/issues/15068)

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 “Tip: Optimizing Performance by Skipping Effects” trong docs/hooks-effect.html, được liên kết trong issue. Xem lại phần giải thích về dependency array [count] và làm rõ cách các phép so sánh hoạt động khi một array có nhiều phần tử. Được xem là hoàn tất khi tài liệu giải thích rõ ràng, không mơ hồ về trường hợp có nhiều phần tử.

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
Đặc tả rõ ràng
Mức phù hợp với người mới
55/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.