cancel the debounce in the cleanup function that actually handles the event setup and teardown, where it belongs

Signed-off-by: Morgan Bentell <mbentell@spotify.com>
This commit is contained in:
Morgan Bentell
2023-03-20 13:27:47 +01:00
parent 6f8035a265
commit 791c7467d4
+3 -6
View File
@@ -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;