fix: Prevent WebSocket reconnection attempts after unmount

Right now, when the WebSocketContext component unmounts,
there is still a pending reconnection attempt that tries
to reconnect the WebSocket after 3 seconds.
This commit is contained in:
Haileyesus
2026-01-31 11:58:46 +03:00
parent 5a4813f9bd
commit eca96c6973

View File

@@ -19,6 +19,7 @@ export const useWebSocket = () => {
const useWebSocketProviderState = () => {
const wsRef = useRef<WebSocket | null>(null);
const unmountedRef = useRef(false);
const [messages, setMessages] = useState<any[]>([]);
const [isConnected, setIsConnected] = useState(false);
const reconnectTimeoutRef = useRef<NodeJS.Timeout | null>(null);
@@ -27,6 +28,7 @@ const useWebSocketProviderState = () => {
connect();
return () => {
unmountedRef.current = true;
if (reconnectTimeoutRef.current) {
clearTimeout(reconnectTimeoutRef.current);
}
@@ -37,6 +39,7 @@ const useWebSocketProviderState = () => {
}, []); // Keep dependency array but add proper cleanup
const connect = () => {
if (unmountedRef.current) return; // Prevent connection if unmounted
try {
const isPlatform = import.meta.env.VITE_IS_PLATFORM === 'true';
@@ -79,6 +82,7 @@ const useWebSocketProviderState = () => {
// Attempt to reconnect after 3 seconds
reconnectTimeoutRef.current = setTimeout(() => {
if (unmountedRef.current) return; // Prevent reconnection if unmounted
connect();
}, 3000);
};