reactjs / reactjs/react.dev

[Suggestion]: Update useEffect cleanup example regarding mount

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

Summary

On the Synchronizing with Effects page under section "Step 3: Add cleanup if needed" it seems like the example should be updated along with adding some more explanation for how useEffect works when in Strict Mode.

Page

https://react.dev/learn/synchronizing-with-effects

Details

Consider the following code:

import { useEffect } from "react";

export default function App() {
  return (
    <div>
      <h1>My App</h1>
      <TestExample />
    </div>
  );
}

function TestExample() {
  useEffect(() => {
    console.log("run effect");
    return () => {
      console.log("cleanup");
    };
  }, []);

  return <h2>Test example!</h2>;
}

From reading the documentation and the explanation for how components mount, unmount, and then mount again in Strict Mode I would expect this example to log "run effect", then log "cleanup", and then log "run effect". Instead it seems like the component is only mounting once and not doing the extra steps because it only logs "run effect" once and nothing else.

After experimenting and searching around, I think the issue is that React can tell that this component will never be unmounted so it does not bother with the extra steps of unmounting and remounting it in Strict Mode. Look at this updated example:

import { useEffect, useState } from "react";

export default function App() {
  const [showTestExample, setShowTestExample] = useState(false);

  return (
    <div>
      <h1>My App</h1>
      <button onClick={() => setShowTestExample(!showTestExample)}>
        Toggle TestExample
      </button>
      {showTestExample ? <TestExample /> : ""}
    </div>
  );
}

function TestExample() {
  useEffect(() => {
    console.log("run effect");
    return () => {
      console.log("cleanup");
    };
  }, []);

  return <h2>Test example!</h2>;
}

Now the component can be toggled to render or be removed from the screen. It now behaves as expected- when clicking the button and adding the component, it console logs "run effect", then "cleanup", then "run effect". I am honestly not sure how the example given in the docs is working at all because it seems like that component will always be rendered. I think this behavior should be explained in the doc so readers can understand why sometimes they may see the mount/unmount/remount behavior and other times they may not. Please correct me if my understanding is wrong.

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 "Step 3: Add cleanup if needed" trên https://react.dev/learn/synchronizing-with-effects và tái hiện hành vi Strict Mode được báo cáo bằng các ví dụ trong issue. Cập nhật ví dụ và phần giải thích để người đọc hiểu khi nào hành vi cleanup và remount xảy ra, sau đó kiểm tra hành vi được ghi lại dựa trên các ví dụ.

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