From 818c17960678bc406af21181ad4914527da338c9 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Sat, 25 Feb 2023 19:52:56 -0600 Subject: [PATCH] Avoid strict mode to prevent event duplication --- src/hooks.ts | 10 +++++----- src/main.tsx | 5 +---- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/hooks.ts b/src/hooks.ts index f3c36df..c197cb1 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -1,4 +1,4 @@ -import { useState } from "react"; +import { useState, useRef } from "react"; import { TypedUseSelectorHook, useDispatch, useSelector } from "react-redux"; import type { RootState, AppDispatch } from "./store"; import { clamp } from "lodash"; @@ -17,7 +17,7 @@ interface Timer { export const useTimer = (length: number, onTimeout?: () => unknown): Timer => { const [segs, setSegs] = useState(0); - const [timeoutId, setTimeoutId] = useState>(); + const timeoutId = useRef>(); const updateTimer = (startTime: number) => { const endTime = startTime + length; @@ -25,7 +25,7 @@ export const useTimer = (length: number, onTimeout?: () => unknown): Timer => { const segs = invLerp(startTime, endTime, currentTime) * 5; setSegs(segs); if (segs > 0) { - setTimeoutId(setTimeout(updateTimer, 100, startTime)); + timeoutId.current = setTimeout(updateTimer, 100, startTime); } else if (onTimeout) { onTimeout(); } @@ -33,11 +33,11 @@ export const useTimer = (length: number, onTimeout?: () => unknown): Timer => { const start = () => { const startTime = Date.now(); - setTimeoutId(setTimeout(updateTimer, 100, startTime)); + timeoutId.current = setTimeout(updateTimer, 100, startTime); }; const cancel = () => { - clearTimeout(timeoutId); + clearTimeout(timeoutId.current); setSegs(0); }; diff --git a/src/main.tsx b/src/main.tsx index 5841dd9..00be960 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -1,12 +1,9 @@ -import React from "react"; import ReactDOM from "react-dom/client"; import App from "./App"; import { setup } from "./socket"; ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render( - - - + ); setup();