From 044d9ad507c6a89ffce30041c81a6335d0e85537 Mon Sep 17 00:00:00 2001 From: ayan4m1 Date: Wed, 7 Feb 2024 13:45:51 -0500 Subject: [PATCH] prevent doubling up on visibilitychange event handlers --- src/hooks/useTimer.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/hooks/useTimer.js b/src/hooks/useTimer.js index d0b5a59..1d876ab 100644 --- a/src/hooks/useTimer.js +++ b/src/hooks/useTimer.js @@ -30,7 +30,12 @@ export default function useTimer(run = true, interval = 1000) { }, [running, incrementTimer, interval]); useEffect(() => { - // if we remove visibilitychange on unmount we lose it, so only set it up once + // if we remove visibilitychange on unmount we lose it, so always remove it + // before adding it to ensure it only exists once + document.removeEventListener( + 'visibilitychange', + handleDocumentVisibilityChange + ); document.addEventListener( 'visibilitychange', handleDocumentVisibilityChange