import React, { useState, useEffect } from 'react';
function DigitalClock() {
const[currentTime, setCurrentTime] = useState(new Date());
useEffect(()=>{
const interval = setInterval(()=>{
setCurrentTime(new Date());
},1000);
return () => clearInterval(interval);
},[]);
useEffect(()=>{
const second = currentTime.getSeconds();
if(second===0){
console.log("another minute passed");
}
},[currentTime])
const formattedTime = currentTime.toLocaleTimeString();
return(
<h2>{formattedTime}</h2>
)
}
export default DigitalClock;
IN THIS I WANT TO MAKE A CONSOLE.LOG AFTER EVERY MINUTE PASSED, BUT IT ONLY PRINTS THE FIRST MINUTE.
I want to know how to solve this issue, and how does useEffect hooks work in Reactjs.
2
Answers
you can modify the
useEffect
hook to check if theseconds
value is divisible by 60 .you can use this code:
I believe you do not need two useEffect to do this. Follow an example: