I'm trying to create a timer with React Hook to use it on a Context. I have no problem when running it using Context and Vanilla JS, but when I use Hooks it seems to be a few miliseconds behind.
My useTimerHook:
import { useState } from 'react';
const useTimerHook = () => {
const [minutes, setMinutes] = useState(0);
const [seconds, setSeconds] = useState(0);
const [miliSeconds, setMiliSeconds] = useState(0);
function write() {
let mt, st, mlst;
setMiliSeconds(miliSeconds + 1);
if (miliSeconds > 99) {
setSeconds(seconds + 1);
setMiliSeconds(0);
}
if (seconds > 59) {
setMinutes(minutes + 1);
setSeconds(0);
}
mlst = ('0' + miliSeconds).slice(-2);
st = ('0' + seconds).slice(-2);
mt = ('0' + minutes).slice(-2);
return `${mt}:${st}:${mlst}`;
}
function reset() {
setMinutes(0);
setMiliSeconds(0);
setSeconds(0);
}
return {
write,
reset,
};
};
My Context:
import useTimerHook from '@hooks/useTimerHook';
const FightContext = createContext();
function FightProvider(props) {
const [time, setTime] = useState('Presiona Start');
const [status, setStatus] = useState(false);
const { write: write, reset: reset } = useTimerHook();
const startFight = () => {
setStatus(true);
};
const endFight = () => {
setStatus(false);
reset();
};
useEffect(() => {
if (status) {
const timer = setTimeout(() => setTime(write()), 10);
return () => {
clearTimeout(timer);
};
}
});
return (
<FightContext.Provider
value={{
time,
startFight,
endFight,
status,
}}
>
{props.children}
</FightContext.Provider>
);
}
export { FightContext, FightProvider };
As I said, when I try to run this the timer is slower than it should be. I.e, when my phone timer is at 15 seconds, the Hook timer is at 13.
I'm pretty sure that I missing something obvious here, or maybe I don't fully understand how Hooks and Context works.