Skip to content

Commit

Permalink
fix(VncConsole): prevent running initialization code more than once (p…
Browse files Browse the repository at this point in the history
…atternfly#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.
  • Loading branch information
KKoukiou committed Nov 16, 2022
1 parent a6cd986 commit e419c8c
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions packages/react-console/src/components/VncConsole/VncConsole.tsx
Expand Up @@ -97,23 +97,23 @@ export const VncConsole: React.FunctionComponent<VncConsoleProps> = ({

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}`;

Expand All @@ -127,7 +127,7 @@ export const VncConsole: React.FunctionComponent<VncConsoleProps> = ({
rfb.current.viewOnly = viewOnly;
rfb.current.scaleViewport = scaleViewport;
rfb.current.resizeSession = resizeSession;
};
}, [addEventListeners, encrypt, rfb, repeaterID, shared, credentials, novncElem]);

React.useEffect(() => {
initLogging(vncLogging);
Expand Down

0 comments on commit e419c8c

Please sign in to comment.