Running an effect only on update vs Strict mode
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ả
I have written a variation of the useEffect hook that should only run on updates. I used a useRef hook to accomplish this the way the FAQ in the old docs recommends it here:
I couldn't find the same piece in the new documentation.
The problem is that the effect still runs on the initial render when strict mode is on:
https://codesandbox.io/s/delicate-lake-88phjz?file=/src/App.js
however it behaves correctly when the strict mode is off:
https://codesandbox.io/s/hungry-wright-tl7p43?file=/src/App.js
In the former example the message is logged to the console right away even though it shouldn't be.
The possible explanations I can think of are:
- I'm doing something wrong. Can you please point the mistake out for me?
- The old docs are off and this is not the right way to do it. In this case can you please let me know what the right way of achieving this behavior is?
- The strict mode is to blame. I do find it irritating at times, if I'm frank, but that's probably unlikely.
Can someone help me with this? Thanks a lot!
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
Bắt đầu bằng cách tái hiện hành vi trong hai ví dụ CodeSandbox được liên kết, so sánh các trường hợp có Strict Mode và không có Strict Mode. Đọc tài liệu hiện tại về useEffect và useRef cùng với phần trích dẫn FAQ cũ; được xem là hoàn tất khi hành vi được khuyến nghị cho các effect bỏ qua lần render ban đầu được ghi rõ trong tài liệu hoặc issue được giải thích là hành vi Strict Mode được mong đợ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ó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Cần làm rõ
- Mức phù hợp với người mới
- 25/100