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;