[Suggestion]: Section "Queueing a Series of State Updates": await in event handler cause immediate rerender
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ả
Summary
The documentation says here: "React waits until all code in the event handlers has run before processing your state updates"
This not the case when using await inside the handler!
Example
const [myNumber, setMyNumber] = useState(0);
async function handleClick(){
setMyNumber(i => i + 1);
await delay(100);
setMyNumber(i => i + 1);
}
function delay(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}
This will rerender the component twice!
Page
https://react.dev/learn/queueing-a-series-of-state-updates
Details
I suggest it should be mentioned as caveat/pitfall that if you call await inside an event handler,
all changes made to states before that await will then be flushed and trigger a rerender immediately, not waiting
for the event handler to finish completely.
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 trang “Queueing a Series of State Updates” được liên kết trong issue và tái hiện ví dụ async event-handler được cung cấp để xác nhận hành vi. Cập nhật trang đó với một lưu ý giải thích rằng các state update trước một await có thể được flush trước khi handler hoàn tất; công việc được xem là hoàn thành khi ví dụ và hướng dẫn về batching chính xác và rõ ràng.
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ó
- 1/5
- Thời gian dự kiến
- Dưới một 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