A customizable countdown component for React.
A customizable countdown component for React.
A customizable countdown component and Hook for React.
v3 is in beta. Install it with
npm install react-countdown@next. The current stable release is documented at the v2.3.6 tag. See the Changelog for what changed and how to migrate.
You can install the module via npm, pnpm, or yarn:
npm install react-countdown --savepnpm add react-countdownyarn add react-countdownThe package ships as both ES modules and CommonJS, is fully typed, and is side-effect free, so bundlers can tree-shake it. Import the component and the useCountdown Hook from the package root:
import Countdown, { useCountdown } from 'react-countdown';If you only need one of them, dedicated entry points let your bundler include just that part (plus the shared core) and leave the rest out:
import Countdown from 'react-countdown/component';
import { useCountdown } from 'react-countdown/hook';This countdown started as part of a small web app. Extracting it into its own package was a way to explore the broader aspects of building with React, testing with Jest, and publishing an open source project.
Here are some examples which you can try directly online. You can also clone this repo and explore some more examples in there by running pnpm start within the examples folder.
A very simple and minimal example of how to set up a countdown that counts down from 10 seconds.
import { createRoot } from 'react-dom/client';
import Countdown from 'react-countdown';
createRoot(document.getElementById('root')).render();In case you want to change the output of the component, or want to signal that the countdown's work is done, you can do this by defining an onComplete callback and/or custom renderer.
import { createRoot } from 'react-dom/client';
import Countdown from 'react-countdown';
// Random component
const Completionist = () => You are good to go!;
// Renderer callback with completed condition
const renderer = ({ hours, minutes, seconds, completed }) => {
return completed ? (
) : (
{hours}:{minutes}:{seconds}
);
};
createRoot(document.getElementById('root')).render(
);Here is an example with a countdown of 10 seconds that displays the total time difference in milliseconds. In order to display the milliseconds appropriately, the intervalDelay value needs to be lower than 1000ms and a precision of 1 to 3 should be used. Last but not least, a simple renderer callback needs to be set up.
import { createRoot } from 'react-dom/client';
import Countdown from 'react-countdown';
createRoot(document.getElementById('root')).render(
}
/>
);A stopwatch is simply a countdown with overtime enabled that starts at the current time: the time delta immediately crosses 0 and keeps running, so the elapsed time counts up. Because the render props' formatted values are based on the absolute time delta, rendering them (instead of relying on the default renderer, which prefixes a - once total turns negative) reads as a regular count-up timer. The countdown's api (start(), pause(), stop()) works unchanged.
import { createRoot } from 'react-dom/client';
import Countdown from 'react-countdown';
createRoot(document.getElementById('root')).render(
(
)}
/>
);Note: because the timer starts at
0, the time delta'stotalis negative andcompletedistruefrom the first tick onwards. Theformattedvalues stay positive, which is why the renderer above doesn't need to handle the sign.
useCountdown HookIf you prefer React Hooks over normal components, you can also use the built-in useCountdown Hook to render and control the countdown.
import { useRef } from 'react';
import { createRoot } from 'react-dom/client';
import { useCountdown } from 'react-countdown';
// Function component
const MyComponent = () => {
const props = useRef({ date: Date.now() + 5000 });
const { hours, minutes, seconds, api } = useCountdown(props.current);
return {api.isCompleted() ? 'Completionist!' : `${hours}:${minutes}:${seconds}`} ;
};
createRoot(document.getElementById('root')).render();Note:
useRefcould be omitted here by settingfreezePropstotrue.
| Name | Type | Default | Description |
|---|---|---|---|
| date | Date|string|number | required | Date or timestamp in the future |
| daysInHours | boolean |
false |
Days are calculated as hours |
| zeroPadTime | number |
2 |
Length of zero-padded output, e.g.: 00:01:02 |
| zeroPadDays | number |
zeroPadTime |
Length of zero-padded days output, e.g.: 01 |
| controlled | boolean |
false |
Hands over the control to its parent(s) |
| intervalDelay | number |
1000 |
Interval delay in milliseconds |
| precision | number |
0 |
The precision on a millisecond basis |
| autoStart | boolean |
true |
Countdown auto-start option |
| overtime | boolean |
false |
Counts down to infinity |
| renderer* | function |
undefined |
Custom renderer callback |
| now | function |
Date.now |
Alternative handler for the current date |
| freezeProps | boolean |
false |
Ignore all prop changes after mount |
| resetKey | string|number | undefined |
Change this value to restart the countdown (component + Hook) |
| onMount | function |
undefined |
Callback when component mounts |
| onStart | function |
undefined |
Callback when countdown starts |
| onPause | function |
undefined |
Callback when countdown pauses |
| onStop | function |
undefined |
Callback when countdown stops |
| onTick | function |
undefined |
Callback on every interval tick (controlled = false) |
| onComplete | function |
undefined |
Callback when countdown ends |
*Not used by the useCountdown.
dateThe date prop is the only required one and can be a Date object, string, or timestamp in the future. By default, this date is compared with the current date, or a custom handler defined via now.
Valid values can be (and more):
'2020-02-01T01:02:03' // Date time string format1580518923000 // Timestamp in millisecondsnew Date(1580518923000) // Date objectdaysInHoursDefines whether the time of day should be calculated as hours rather than separated days.
zeroPadTimeThis option defaults to 2 in order to display the common format 00:00:00 instead of 0:0:0. Minutes and seconds are always capped at a padding width of 2. A value higher than 2 only widens the leading field: the hours when daysInHours is true (since hours then absorb the days and can exceed 99), otherwise the separate days field (see zeroPadDays). If the value is lower, the output won't be zero-padded like the example before is showing.
zeroPadDaysDefaults to zeroPadTime. It works the same way as zeroPadTime does, just for days.
controlledCan be useful if the countdown's interval and/or date control should be handed over to the parent. In case controlled is true, the
provided date will be treated as the countdown's actual time difference and not be compared to now anymore.
intervalDelayDefaults to 1000ms. With higher [`pre
No open issues yet, or sync has not completed.