1

I am trying to implement react countdown timer for my app OTP with minutes and seconds. I could able to show seconds but was unable to display with minutes.

for example[mm:ss] 02:30

This is my implementation with some reference

import React, { useEffect, useState } from 'react';
import './timer.scss';

const Timer = ({ seconds }) => {
    const [timeLeft, setTimeLeft] = useState(seconds);

    useEffect(() => {
        if (!timeLeft) return;
        const intervalId = setInterval(() => {
            setTimeLeft(timeLeft - 1);
        }, 1000);
        return () => clearInterval(intervalId);
    }, [timeLeft]);

    return (
        <div>
            <span>{`Time ends in: ${timeLeft}`}</span>
        </div>
    );
};

export default Timer;
skyboyer
  • 22,209
  • 7
  • 57
  • 64
iOS Coder
  • 39
  • 6

0 Answers0