admin管理员组文章数量:1355648
I'm working on a React app where I need to establish a WebSocket connection to receive real-time updates. However, I'm facing issues with connections not closing properly when the component unmounts.
Here’s my current approach:
import { useEffect, useState } from "react";
const useWebSocket = (url: string) => {
const [messages, setMessages] = useState<string[]>([]);
useEffect(() => {
const socket = new WebSocket(url);
socket.onmessage = (event) => {
setMessages((prev) => [...prev, event.data]);
};
return () => {
socket.close();
};
}, [url]);
return messages;
};
export default useWebSocket;
The problem:
Sometimes, the WebSocket connection remains open even after navigating away.
In some cases, I receive duplicate messages when re-opening the same page.
How can I ensure that the WebSocket connection is managed properly to avoid memory leaks and duplicate messages?
Any guidance or best practices would be greatly appreciated!
I'm working on a React app where I need to establish a WebSocket connection to receive real-time updates. However, I'm facing issues with connections not closing properly when the component unmounts.
Here’s my current approach:
import { useEffect, useState } from "react";
const useWebSocket = (url: string) => {
const [messages, setMessages] = useState<string[]>([]);
useEffect(() => {
const socket = new WebSocket(url);
socket.onmessage = (event) => {
setMessages((prev) => [...prev, event.data]);
};
return () => {
socket.close();
};
}, [url]);
return messages;
};
export default useWebSocket;
The problem:
Sometimes, the WebSocket connection remains open even after navigating away.
In some cases, I receive duplicate messages when re-opening the same page.
How can I ensure that the WebSocket connection is managed properly to avoid memory leaks and duplicate messages?
Any guidance or best practices would be greatly appreciated!
Share Improve this question asked Mar 28 at 5:45 Sandro TushurashviliSandro Tushurashvili 436 bronze badges1 Answer
Reset to default 1You can close the existing connection before creating a new connection -
import { useEffect, useState, useRef } from "react";
const useWebSocket = (url: string) => {
const [messages, setMessages] = useState<string[]>([]);
const webSocketRef = useRef<any>(null);
useEffect(() => {
if (webSocketRef.current) {
webSocketRef.current.close();
}
const socket = new WebSocket(url);
webSocketRef.current = socket;
socket.onmessage = (event) => {
setMessages((prev) => [...prev, event.data]);
};
return () => {
if (webSocketRef.current) {
webSocketRef.current.close();
}
};
}, [url]);
return messages;
};
export default useWebSocket;
本文标签: reactjsHow to properly handle WebSocket connections in a React app with useEffectStack Overflow
版权声明:本文标题:reactjs - How to properly handle WebSocket connections in a React app with useEffect? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744054475a2583021.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论