From 791c7467d44dfd32d073609e43c60f00584d5b05 Mon Sep 17 00:00:00 2001 From: Morgan Bentell Date: Mon, 20 Mar 2023 13:27:47 +0100 Subject: [PATCH] cancel the debounce in the cleanup function that actually handles the event setup and teardown, where it belongs Signed-off-by: Morgan Bentell --- plugins/techdocs-react/src/hooks.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/plugins/techdocs-react/src/hooks.ts b/plugins/techdocs-react/src/hooks.ts index d084a91cf5..2984a4b588 100644 --- a/plugins/techdocs-react/src/hooks.ts +++ b/plugins/techdocs-react/src/hooks.ts @@ -82,18 +82,15 @@ export const useShadowRootSelection = (waitMillis: number = 0) => { [shadowRoot, setSelection, waitMillis], ); - useEffect(() => { - // cancel debounce on unmount - return () => handleSelectionChange.cancel(); - }, [handleSelectionChange]); - useEffect(() => { window.document.addEventListener('selectionchange', handleSelectionChange); - return () => + return () => { + handleSelectionChange.cancel(); window.document.removeEventListener( 'selectionchange', handleSelectionChange, ); + }; }, [handleSelectionChange]); return selection;