Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/hooks/useTouchMove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ export default function useTouchMove(
touchEventsRef.current = { onTouchStart, onTouchMove, onTouchEnd, onWheel };

React.useEffect(() => {
const abortController = new AbortController();

function onProxyTouchStart(e: TouchEvent) {
touchEventsRef.current.onTouchStart(e);
}
Expand All @@ -123,16 +125,26 @@ export default function useTouchMove(
touchEventsRef.current.onWheel(e);
}

document.addEventListener('touchmove', onProxyTouchMove, { passive: false });
document.addEventListener('touchend', onProxyTouchEnd, { passive: true });

// No need to clean up since element removed
ref.current.addEventListener('touchstart', onProxyTouchStart, { passive: true });
ref.current.addEventListener('wheel', onProxyWheel, { passive: false });
document.addEventListener('touchmove', onProxyTouchMove, {
passive: false,
signal: abortController.signal,
});
document.addEventListener('touchend', onProxyTouchEnd, {
passive: true,
signal: abortController.signal,
});

ref.current.addEventListener('touchstart', onProxyTouchStart, {
passive: true,
signal: abortController.signal,
});
ref.current.addEventListener('wheel', onProxyWheel, {
passive: false,
signal: abortController.signal,
});
Comment on lines +137 to +144
Copy link

Copilot AI Nov 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential null reference error: ref.current could be null when the effect runs or during cleanup. The code should check if ref.current exists before adding event listeners to prevent runtime errors.

Consider adding a null check:

if (ref.current) {
  ref.current.addEventListener('touchstart', onProxyTouchStart, {
    passive: true,
    signal: abortController.signal,
  });
  ref.current.addEventListener('wheel', onProxyWheel, {
    passive: false,
    signal: abortController.signal,
  });
}
Suggested change
ref.current.addEventListener('touchstart', onProxyTouchStart, {
passive: true,
signal: abortController.signal,
});
ref.current.addEventListener('wheel', onProxyWheel, {
passive: false,
signal: abortController.signal,
});
if (ref.current) {
ref.current.addEventListener('touchstart', onProxyTouchStart, {
passive: true,
signal: abortController.signal,
});
ref.current.addEventListener('wheel', onProxyWheel, {
passive: false,
signal: abortController.signal,
});
}

Copilot uses AI. Check for mistakes.

return () => {
document.removeEventListener('touchmove', onProxyTouchMove);
document.removeEventListener('touchend', onProxyTouchEnd);
abortController.abort();
};
}, []);
}
Loading