reactjs / reactjs/react.dev

Use a better example for Effect hook

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

The following example is used in:

import React, { useState, useEffect } from 'react';

function Example() {
  const [count, setCount] = useState(0);

  // Similar to componentDidMount and componentDidUpdate:
  useEffect(() => {
    // Update the document title using the browser API
    document.title = `You clicked ${count} times`;
  });

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

This example creates confusion because the example doesn't actually need useEffect to operate correctly.

If you set document.title outside of the useEffect, the app behaves exactly the same.


Suggested changes

  • Use an example which would not work correctly if it wasn't placed in a useEffect. It should be something that depends on the DOM having been updated. Maybe setting the document.title to a document.getElementById or something like that. This would also be a contrived/useless example. Something practical would be better.

  • Show the lifecycle of useEffect early. I was most interested in the lifecycle of when this method is called. In Dan's recent blog post on effects, this is the first thing that is covered, and it should be the same for these docs.

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 các ví dụ về Effect Hook trong hooks-overview.html#effect-hook và hooks-effect.html, sau đó so sánh ví dụ hiện tại với phần thảo luận về vòng đời trong tài liệu được tham chiếu. Thay thế ví dụ gây khó hiểu bằng một ví dụ minh họa vì sao cần useEffect và giới thiệu vòng đời của nó sớm hơn; được xem là hoàn tất khi cả hai trang đều giải thích nhất quán về hành vi và hiển thị ví dụ đã cải thiện.

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