Docs regarding setState are in direct conflict with one another
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ả
In Introducting Hooks, and several other places, there is a simple counter example that illustrates how to use the useState hook.
function Example() {
// Declare a new state variable, which we'll call "count"
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
However, there happens to be another counter example in the Hooks API Reference for useState.
function Counter({initialCount}) {
const [count, setCount] = useState(initialCount);
return (
<>
Count: {count}
<button onClick={() => setCount(initialCount)}>Reset</button>
<button onClick={() => setCount(prevCount => prevCount - 1)}>-</button>
<button onClick={() => setCount(prevCount => prevCount + 1)}>+</button>
</>
);
}
In this second example, a callback is being passed into the setState function, and the code is accompanied by the following language...
The ”+” and ”-” buttons use the functional form, because the updated value is based on the previous value.
This would imply that the first example (which is in a very prominent location in the docs by the way), is actually incorrect and is encouraging an anti-pattern.
I hope you can see how these two examples are in direct conflict with one another and might be causing confusion in the community!
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Đọc các ví dụ về bộ đếm trong Introducing Hooks và Hooks API Reference, đặc biệt là phần functional-updates, rồi so sánh hướng dẫn về setState. Giải quyết sự không nhất quán để tài liệu đưa ra một giải thích rõ ràng về thời điểm phù hợp để sử dụng từng dạng, sau đó xác minh rằng các ví dụ được liên kết và phần văn bản xung quanh không còn mâu thuẫn với nhau.
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
- 35/100