Skip to content

Instantly share code, notes, and snippets.

@rezaerami
Last active June 23, 2021 17:15
Show Gist options
  • Save rezaerami/c4e4cea468322b2034ea4ebf3868d859 to your computer and use it in GitHub Desktop.
Save rezaerami/c4e4cea468322b2034ea4ebf3868d859 to your computer and use it in GitHub Desktop.
this is a working countdown, but it has some issues
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;
@rezaerami
Copy link
Author

please do not comment here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment