React Countdown Component
react-countdown is a highly customizable React component designed to display countdowns. It is currently stable at version 2.3.6 and has seen active maintenance, with multiple bugfix and minor feature releases within the 2.x series. The library allows developers to define a target date and provides flexible rendering options, including a custom `renderer` function or rendering a child component upon completion. Key features include support for displaying milliseconds with configurable precision, an `overtime` prop to allow negative countdowns, and an API to programmatically `stop`, `isStarted`, and `isStopped` the countdown. It ships with TypeScript types, ensuring type safety for React applications. This package differentiates itself through its extensive customization capabilities and robust API for managing countdown state.
Common errors
-
Property 'date' is missing in type 'CountdownProps' but required in type 'Readonly<CountdownProps>'.
cause The `date` prop was made required for the `Countdown` component starting from version 2.3.3.fixEnsure the `date` prop is always provided, e.g., `<Countdown date={Date.now() + 10000} />`. -
TypeError: Cannot read properties of undefined (reading 'total') (when trying to show milliseconds)
cause Attempting to render `props.total` (milliseconds) in a custom renderer without setting `intervalDelay` and `precision` correctly.fixAdd `intervalDelay={0}` (or a small number like `10`) and `precision={3}` (for 3 decimal places of milliseconds) to the `Countdown` component props. -
TypeError: Countdown is not a constructor (or similar errors like `_reactCountdown.Countdown is not a function`)
cause Incorrectly importing `Countdown` as a named export when it is a default export.fixChange the import statement from `import { Countdown } from 'react-countdown';` to `import Countdown from 'react-countdown';`.
Warnings
- breaking The `date` prop on the `Countdown` component transitioned from being optional to required.
- gotcha Displaying milliseconds accurately requires specific `intervalDelay` and `precision` prop configurations.
- gotcha The `onComplete` callback might not consistently trigger immediately if the countdown `date` is already in the past at component initialization in older versions.
- deprecated Internal usage of `React.Props` type was removed due to deprecation within React itself.
Install
-
npm install react-countdown -
yarn add react-countdown -
pnpm add react-countdown
Imports
- Countdown
import { Countdown } from 'react-countdown';import Countdown from 'react-countdown';
- CountdownProps
import type { CountdownProps } from 'react-countdown'; - CountdownRendererFn
import type { CountdownRendererFn } from 'react-countdown';
Quickstart
import React from 'react';
import ReactDOM from 'react-dom/client';
import Countdown from 'react-countdown';
// Create a root
const root = ReactDOM.createRoot(document.getElementById('root'));
const Completionist = () => <span>Time's up!</span>;
const renderer = ({ hours, minutes, seconds, completed }) => {
if (completed) {
return <Completionist />;
} else {
return <span>{hours}:{minutes}:{seconds}</span>;
}
};
root.render(
<React.StrictMode>
<Countdown
date={Date.now() + 10000} // Countdown for 10 seconds
renderer={renderer}
onComplete={() => console.log('Countdown finished!')}
/>
</React.StrictMode>
);