Answer the question
In order to leave comments, you need to log in
Correct type to work with setInterval?
Snippet of my code. Implements a timer. I get an error
Property 'current' is missing in type 'Timeout' but required in type 'MutableRefObject'
const [timerMinutes, setTimerMinutes] = useState('00');
const [timerSeconds, setTimerSeconds] = useState('00');
let interval = useRef(null);
let time = 10;
const startTimer = () => {
interval = setInterval(() => { // здесь ошибка
const now = new Date().getTime();
const minutes = String(Math.floor((time % (1000 * 60 * 60)) / (1000 * 60)));
const seconds = String(Math.floor((time % (1000 * 60 * 60)) / 1000));
if(time < 0) {
// stop the timer
clearInterval(interval.current); // здесь ошибка
} else {
setTimerMinutes(minutes);
setTimerSeconds(seconds);
}
},1000);
};
Answer the question
In order to leave comments, you need to log in
https://reactjs.org/docs/hooks-reference.html#useref
const interval = useRef(null);
// ...
interval.current = setInterval(//...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question