From e419c8c3e7cdc0d0d3e1329bf26b7c27c1f26807 Mon Sep 17 00:00:00 2001 From: Katerina Koukiou Date: Wed, 16 Nov 2022 18:22:56 +0100 Subject: [PATCH] fix(VncConsole): prevent running initialization code more than once (#8373) The functions that were on the dependency list of the useEffect hook got redefined each time the component got updated, resulting in the component's initialization code, from within the useEffect hook, to be running more than once. That was unwanted behavior and the useCallback wrappers should prevent it. --- .../src/components/VncConsole/VncConsole.tsx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/react-console/src/components/VncConsole/VncConsole.tsx b/packages/react-console/src/components/VncConsole/VncConsole.tsx index c8ac73999e2..b15fb90058d 100644 --- a/packages/react-console/src/components/VncConsole/VncConsole.tsx +++ b/packages/react-console/src/components/VncConsole/VncConsole.tsx @@ -97,23 +97,23 @@ export const VncConsole: React.FunctionComponent = ({ const [status, setStatus] = React.useState(CONNECTING); - const addEventListeners = () => { + const addEventListeners = React.useCallback(() => { if (rfb.current) { rfb.current?.addEventListener('connect', onConnected); rfb.current?.addEventListener('disconnect', _onDisconnected); rfb.current?.addEventListener('securityfailure', _onSecurityFailure); } - }; + }, [rfb]); - const removeEventListeners = () => { + const removeEventListeners = React.useCallback(() => { if (rfb.current) { rfb.current.removeEventListener('connect', onConnected); rfb.current.removeEventListener('disconnect', _onDisconnected); rfb.current.removeEventListener('securityfailure', _onSecurityFailure); } - }; + }, [rfb]); - const connect = () => { + const connect = React.useCallback(() => { const protocol = encrypt ? 'wss' : 'ws'; const url = `${protocol}://${host}:${port}/${path}`; @@ -127,7 +127,7 @@ export const VncConsole: React.FunctionComponent = ({ rfb.current.viewOnly = viewOnly; rfb.current.scaleViewport = scaleViewport; rfb.current.resizeSession = resizeSession; - }; + }, [addEventListeners, encrypt, rfb, repeaterID, shared, credentials, novncElem]); React.useEffect(() => { initLogging(vncLogging);