I have a functional component that is supposed to be a running clock:
import React,{useState,useEffect} from 'react';
import 'materialize-css/dist/css/materialize.min.css';
import { parseTime } from '../../Utils/utils'
const MainClock = (props) => {
const [timeString, setTimeString] = useState(parseTime(new Date(), true));
function tick(){
console.log("TICK:" + timeString)
setTimeString(parseTime(new Date(), true));
};
useEffect(()=>{console.log("rendered!");setTimeout(tick,500);},[timeString]);
return (
<div>
<h5 className="center-align mainclock">{timeString}</h5>
</div>
);
}
export default MainClock;
But for some reason it is only being rendered twice and the console output is:
rendered!
TICK:14:56:21
rendered!
TICK:14:56:22
Why isn't useeffect being called after the second render?
Any help is welcomed!
Edit: If it helps, this is parseTime
:
const parseTime = (timeDate, withSeconds=false) =>{
let time = timeDate.getHours()<10 ? `0${timeDate.getHours()}`:`${timeDate.getHours()}`;
time+=":";
time+= timeDate.getMinutes()<10 ? `0${timeDate.getMinutes()}`:`${timeDate.getMinutes()}`;
if(withSeconds){
time+=":";
time+=timeDate.getSeconds()<10 ? `0${timeDate.getSeconds()}`:`${timeDate.getSeconds()}`;
}
return time;
}