M
M
MarEeeeeee2021-05-25 18:53:26
typescript
MarEeeeeee, 2021-05-25 18:53:26

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'

Tell me how to fix it?

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

1 answer(s)
D
Dmitry Belyaev, 2021-05-26
@bingo347

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 question

Ask a Question

731 491 924 answers to any question