reactjs / reactjs/react.dev

[Suggestion]: StrictMode confusion about ordering of the events

Đang mở
#7,315 9 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

This thread comes really close to avoid repeating this question again.
However I still have some doubts about the chain of the events in the strict mode.

I will quote the answer which provide the most valuable feedback.

"The mental model for this is that React really does unmount the component, but when re-mounting the component we restore the previously used state instead of starting fresh, the way Fast Refresh does, or some future features will."

But if that's the case why the order of the events(rendering, setting state, effects etc) is different while running in strict mode and outside of it?

Here are the concrete examples:

  1. https://codesandbox.io/p/sandbox/2z4wq2. This sandbox runs inside a strict mode and by inspecting the console we can see that the order of the events is like this
render
state setter
render
state setter
effect
clean up
effect

The second render and state setter are dimmed out according to the documentation and that's fine. So at least on the surface it appears that react is triggering render function (and state setter) 2 times before moving to the effects.

  1. https://codesandbox.io/p/sandbox/93zfkw. This sandbox runs outside of the strict mode and there is a button to toggle the mounting of the component. If the component is mounted, unmounted and mounted again the order of the events is like this.
render
state setter
effect
clean up
render
state setter
effect

This example has the mental model I have always worked with and which is clear, so I would waste too much time on it since that's how React also behaves on production.

The question is, if react is really performing mounting, unmounting and remounting while is the order different?

To be honest I haven't noticed that this difference is making my code harder to reason about while working with StrictMode.
I would like to know the details, if possible. What does this approach help React achieve that original one does not?

Page

https://react.dev/reference/react/StrictMode

Details

I think it would be helpful to update the page to explain why the behavior in StrictMode is different than simply running mounting, unmounting and mounting again.

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 trang tham chiếu React StrictMode và so sánh phần giải thích hiện tại với chuỗi thảo luận của issue được liên kết cùng hai ví dụ CodeSandbox. Làm rõ vì sao thứ tự quan sát được của render, state, effect và cleanup trong StrictMode khác với một chu kỳ mount-unmount-remount riêng biệt, rồi cập nhật trang để phần giải thích trả lời được sự khác biệt đó.

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