Last active
June 23, 2021 17:15
-
-
Save rezaerami/c4e4cea468322b2034ea4ebf3868d859 to your computer and use it in GitHub Desktop.
this is a working countdown, but it has some issues
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from "react"; | |
import "./styles.css"; | |
const Countdown = ({ dueDate }) => { | |
const [remaningTime, setRemaining] = useState(); | |
console.log("re-render"); | |
useEffect(() => { | |
setRemaining(getDateDiff(dueDate)); | |
setInterval(handleCountdown, 1000); | |
}); | |
const getDateDiff = (dateTime) => new Date(dateTime).getTime() - Date.now(); | |
const handleCountdown = () => { | |
setRemaining(getDateDiff(dueDate)); | |
}; | |
const getCountdown = () => { | |
const second = 1000; | |
const minute = second * 60; | |
const hour = minute * 60; | |
const day = hour * 24; | |
return { | |
hours: Math.floor((remaningTime % day) / hour), | |
minutes: Math.floor((remaningTime % hour) / minute), | |
seconds: Math.floor((remaningTime % minute) / second) | |
}; | |
}; | |
const { hours, minutes, seconds } = getCountdown(); | |
return ( | |
<div className="App"> | |
<h1>Remaining Date</h1> | |
<h2> | |
{hours} : {minutes} : {seconds} | |
</h2> | |
</div> | |
); | |
}; | |
Countdown.defaultProps = { | |
dueDate: Date.now() + 60 * 60 * 1000 | |
}; | |
export default Countdown; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
please do not comment here