title: Countdown Timer in React
See [this greenroots blog](https://blog.greenroots.info/how-to-create-a-countdown-timer-using-react-hooks).
The trick is to have a `setInterval` inside a `useEffect` that calls `setCountDown`.
```jsx
import { useEffect, useState } from 'react';
const useCountdown = (targetDate) => {
const countDownDate = new Date(targetDate).getTime();
const [countDown, setCountDown] = useState(
countDownDate - new Date().getTime()
);
useEffect(() => {
const interval = setInterval(() => {
setCountDown(countDownDate - new Date().getTime());
}, 1000);
return () => clearInterval(interval);
}, [countDownDate]);
return getReturnValues(countDown);
};
const getReturnValues = (countDown) => {
// calculate time left
const days = Math.floor(countDown / (1000 * 60 * 60 * 24));
const hours = Math.floor(
(countDown % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
);
const minutes = Math.floor((countDown % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((countDown % (1000 * 60)) / 1000);
return [days, hours, minutes, seconds];
};
export { useCountdown };
```
```jsx
import React from 'react';
import { useCountdown } from './hooks/useCountdown';
const CountdownTimer = ({ targetDate }) => {
const [days, hours, minutes, seconds] = useCountdown(targetDate);
if (days + hours + minutes + seconds <= 0) {
return
{value}
{type}