Avoid strict mode to prevent event duplication
This commit is contained in:
parent
de3a767425
commit
818c179606
10
src/hooks.ts
10
src/hooks.ts
@ -1,4 +1,4 @@
|
|||||||
import { useState } from "react";
|
import { useState, useRef } from "react";
|
||||||
import { TypedUseSelectorHook, useDispatch, useSelector } from "react-redux";
|
import { TypedUseSelectorHook, useDispatch, useSelector } from "react-redux";
|
||||||
import type { RootState, AppDispatch } from "./store";
|
import type { RootState, AppDispatch } from "./store";
|
||||||
import { clamp } from "lodash";
|
import { clamp } from "lodash";
|
||||||
@ -17,7 +17,7 @@ interface Timer {
|
|||||||
|
|
||||||
export const useTimer = (length: number, onTimeout?: () => unknown): Timer => {
|
export const useTimer = (length: number, onTimeout?: () => unknown): Timer => {
|
||||||
const [segs, setSegs] = useState(0);
|
const [segs, setSegs] = useState(0);
|
||||||
const [timeoutId, setTimeoutId] = useState<ReturnType<typeof setTimeout>>();
|
const timeoutId = useRef<ReturnType<typeof setTimeout>>();
|
||||||
|
|
||||||
const updateTimer = (startTime: number) => {
|
const updateTimer = (startTime: number) => {
|
||||||
const endTime = startTime + length;
|
const endTime = startTime + length;
|
||||||
@ -25,7 +25,7 @@ export const useTimer = (length: number, onTimeout?: () => unknown): Timer => {
|
|||||||
const segs = invLerp(startTime, endTime, currentTime) * 5;
|
const segs = invLerp(startTime, endTime, currentTime) * 5;
|
||||||
setSegs(segs);
|
setSegs(segs);
|
||||||
if (segs > 0) {
|
if (segs > 0) {
|
||||||
setTimeoutId(setTimeout(updateTimer, 100, startTime));
|
timeoutId.current = setTimeout(updateTimer, 100, startTime);
|
||||||
} else if (onTimeout) {
|
} else if (onTimeout) {
|
||||||
onTimeout();
|
onTimeout();
|
||||||
}
|
}
|
||||||
@ -33,11 +33,11 @@ export const useTimer = (length: number, onTimeout?: () => unknown): Timer => {
|
|||||||
|
|
||||||
const start = () => {
|
const start = () => {
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
setTimeoutId(setTimeout(updateTimer, 100, startTime));
|
timeoutId.current = setTimeout(updateTimer, 100, startTime);
|
||||||
};
|
};
|
||||||
|
|
||||||
const cancel = () => {
|
const cancel = () => {
|
||||||
clearTimeout(timeoutId);
|
clearTimeout(timeoutId.current);
|
||||||
setSegs(0);
|
setSegs(0);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,12 +1,9 @@
|
|||||||
import React from "react";
|
|
||||||
import ReactDOM from "react-dom/client";
|
import ReactDOM from "react-dom/client";
|
||||||
import App from "./App";
|
import App from "./App";
|
||||||
import { setup } from "./socket";
|
import { setup } from "./socket";
|
||||||
|
|
||||||
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
|
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
|
||||||
<React.StrictMode>
|
<App />
|
||||||
<App />
|
|
||||||
</React.StrictMode>
|
|
||||||
);
|
);
|
||||||
|
|
||||||
setup();
|
setup();
|
||||||
|
Loading…
Reference in New Issue
Block a user