Scope and use of useEffectEvent in React

React introduces useEffectEvent which was previously called useEvent in experimental mode. Though it states that it adds ton of advantages. I can’t find any sufficient examples to support this. In the React documentation site the challenges they mentioned at the end of the page can easily be solved except one.

The first challenge of fixing static timer(Challenge 2 of 4) solves well with useEffectEvent, since we do not have to add increment as dependency since it takes it in runtime. The functions declared inside this hook doesn’t take snapshot of the states and props during first render and attaches it entirely with an event. If that is the case, there is no need to add even a state updater inside the function. Also how does React does this? I know that when an event listener is added to an event the fixed reference value of the function in that render is attached with the event. Since using this hook makes React using the latest state and props. How and where does React stores it?

Also in Challenge 4 of 4 of Fixing a Delayed Notification, the use of useEffectEvent is solely to abide by the dependency linter. This is the code in documentation:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const onConnected = useEffectEvent(connectedRoomId => {
showNotification('Welcome to ' + connectedRoomId, theme);
});
useEffect(() => {
const connection = createConnection(serverUrl, roomId);
connection.on('connected', () => {
setTimeout(() => {
onConnected(roomId);
}, 2000);
});
connection.connect();
return () => connection.disconnect();
}, [roomId]);
</code>
<code>const onConnected = useEffectEvent(connectedRoomId => { showNotification('Welcome to ' + connectedRoomId, theme); }); useEffect(() => { const connection = createConnection(serverUrl, roomId); connection.on('connected', () => { setTimeout(() => { onConnected(roomId); }, 2000); }); connection.connect(); return () => connection.disconnect(); }, [roomId]); </code>
const onConnected = useEffectEvent(connectedRoomId => {
    showNotification('Welcome to ' + connectedRoomId, theme);
  });

  useEffect(() => {
    const connection = createConnection(serverUrl, roomId);
    connection.on('connected', () => {
      setTimeout(() => {
        onConnected(roomId);
      }, 2000);
    });
    connection.connect();
    return () => connection.disconnect();
  }, [roomId]);

And here is the code without useEffectEvent:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>3. useEffect(() => {
4. const connection = createConnection(serverUrl, roomId);
5. connection.on('connected', () => {
6. setTimeout(() => {
7. showNotification('Welcome to ' + roomId, theme);
8. }, 2000);
9. });
10. connection.connect();
11. return () => connection.disconnect();
12. }, [roomId]);
</code>
<code>3. useEffect(() => { 4. const connection = createConnection(serverUrl, roomId); 5. connection.on('connected', () => { 6. setTimeout(() => { 7. showNotification('Welcome to ' + roomId, theme); 8. }, 2000); 9. }); 10. connection.connect(); 11. return () => connection.disconnect(); 12. }, [roomId]); </code>
3.  useEffect(() => {
4.      const connection = createConnection(serverUrl, roomId);
5.      connection.on('connected', () => {
6.        setTimeout(() => {
7.          showNotification('Welcome to ' + roomId, theme);
8.        }, 2000);
9.      });
10.     connection.connect();
11.     return () => connection.disconnect();
12.   }, [roomId]);

Can we say that the introduction of useEffectEvent is primarily for 2 reasons:

  1. To abide by the dependency linter.
  2. To make a way out of how React works i.e every render has its own version of event handlers so attaching it, would request the user to add a dependency and hence using this Hook, the updated version of the function is called with current state and props?

New contributor

quest is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật