Skip to content

Commit

Permalink
remove event listener + cancel animation frame in handler
Browse files Browse the repository at this point in the history
  • Loading branch information
wardoost committed Mar 25, 2019
1 parent 8e2f342 commit 81cf157
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/useMouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const useMouse = (ref: RefObject<HTMLElement>): State => {

useEffect(() => {
const handler = (event: MouseEvent) => {
cancelAnimationFrame(frame.current)
frame.current = requestAnimationFrame(() => {
if (ref && ref.current) {
const {left, top} = ref.current.getBoundingClientRect()
Expand All @@ -49,11 +50,8 @@ const useMouse = (ref: RefObject<HTMLElement>): State => {
document.addEventListener('mousemove', handler);

return () => {
if (frame.current) {
cancelAnimationFrame(frame.current);
}

document.addEventListener('mousemove', handler);
cancelAnimationFrame(frame.current);
document.removeEventListener('mousemove', handler);
};
}, []);

Expand Down

0 comments on commit 81cf157

Please sign in to comment.