reactjs / reactjs/react.dev

improve documentation for useEffect hook

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

Chưa có ai nhận issue này.

type: documentation
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ả

useEffect takes an array of values that may cause the effect to rerun if they "change".

Two comments: First, what is "change?" From this issue I learned that React uses Object.is to that end, but the documentation for conditionally firing an effect does not state this.

Second, here's what the documentation says:

To implement this, pass a second argument to useEffect that is the array of values that the effect depends on. Our updated example now looks like this:

useEffect(
  () => {
    const subscription = props.source.subscribe();
    return () => {
      subscription.unsubscribe();
    };
  },
  [props.source],
);

Now the subscription will only be recreated when props.source changes.

The wording "when props.source changes is confusing and ambiguous (in my opinion).
For instance, if props.source refers to an object with property x, and some other code, or even code in the same scope, does props.source.x++, then did props.source change?

I think - correct me if I'm wrong - or better, update the documentation - I think that what is meant if the value passed at one call to useEffect() is different from the value passed at another call that happens during a subsequent call to the function in which the call to useEffect is embedded, then the value in this position of the array is considered to have changed. Two values v1 and v2 are considered different iff Object.is(v1, v2) returns false. React does not try to somehow "watch" any objects that are being passed to useEffect's dependency array.
I think this follows from thinking about how useEffect could possibly be implemented, but it would help the reader to state it clearly.
It would also help if the notion of "change" is more clearly defined as "change across calls to the function in which the call to useEffect is embedded, " in my opinion.

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 bằng cách đọc phần được liên kết “Conditionally firing an effect” và issue #15154 để xem phần giải thích hiện có về việc so sánh dependency. Làm rõ rằng React so sánh các giá trị dependency giữa các lần render bằng Object.is và không theo dõi các mutation bên trong những object được tham chiếu; công việc được xem là hoàn tất khi tài liệu useEffect loại bỏ sự mơ hồ xung quanh “thay đổi”.

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
42/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.