module Timer = {
@react.component
let make = (~duration: timerProps) => {
// let (minutes, setMinutes) = React.useState(_ => duration)
// let (seconds, setSeconds) = React.useState(_ => 0)
let ((minutes, seconds), setCountdown) = React.useState(_ => (
duration.initMins,
duration.initSecs,
))
let interval = ref(Js.Nullable.null)
React.useEffect(() => {
let tick = () => {
Js.log3("##", minutes, seconds)
if minutes === 0 && seconds === 0 {
Js.Nullable.iter(interval.contents, (. intervalId) => Js.Global.clearInterval(intervalId))
} else if seconds == 0 {
setCountdown(_ => (minutes - 1, 60))
} else {
setCountdown(_ => (minutes, seconds - 1))
}
}
interval := Js.Nullable.return(Js.Global.setInterval(intervalid => tick(intervalid), 1000))
// Js.Nullable.iter(interval.contents, (. intervalId) => Js.Global.clearInterval(intervalId))
None
})
<div>
{minutes === 0 && seconds === 0
? React.null
: <h1>
{React.string(`${Belt.Int.toString(minutes)} : `)}
{seconds < 10
? React.string(`0${Belt.Int.toString(seconds)}`)
: React.string(Belt.Int.toString(seconds))}
</h1>}
</div>
}
}
My countdown is not working as expected. This is counting minutes back from 60 every second