Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

react-countdown

> 前端框架
Open source

A customizable countdown component for React.

789 stars0 likes3 views
WebsiteGitHub

About

A customizable countdown component for React.

React <Countdown /> [![npm][npm]][npm-url]

A customizable countdown component and Hook for React.

  • Getting Started
  • Motivation
  • Examples
  • Props
  • Hook
  • Lifecycle Callbacks
  • API Reference
  • Helpers
  • FAQ
  • Changelog
  • Contributing
  • License

Getting Started

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:

bash
npm install react-countdown --save
bash
pnpm add react-countdown
bash
yarn add react-countdown

Imports

The 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:

javascript
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:

javascript
import Countdown from 'react-countdown/component';
import { useCountdown } from 'react-countdown/hook';

Motivation

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.

Examples

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.

Basic Usage

A very simple and minimal example of how to set up a countdown that counts down from 10 seconds.

javascript
import { createRoot } from 'react-dom/client';
import Countdown from 'react-countdown';

createRoot(document.getElementById('root')).render();

Live Demo

Custom & Conditional Rendering

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.

javascript
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(
  
);

Live Demo

Countdown in Milliseconds

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.

javascript
import { createRoot } from 'react-dom/client';
import Countdown from 'react-countdown';

createRoot(document.getElementById('root')).render(
   
}
  />
);

Live Demo

Building a Stopwatch

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.

javascript
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's total is negative and completed is true from the first tick onwards. The formatted values stay positive, which is why the renderer above doesn't need to handle the sign.

Using the useCountdown Hook

If you prefer React Hooks over normal components, you can also use the built-in useCountdown Hook to render and control the countdown.

javascript
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: useRef could be omitted here by setting freezeProps to true.

Live Demo

Props

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.

date

The 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 format
  • 1580518923000 // Timestamp in milliseconds
  • new Date(1580518923000) // Date object

daysInHours

Defines whether the time of day should be calculated as hours rather than separated days.

zeroPadTime

This 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.

zeroPadDays

Defaults to zeroPadTime. It works the same way as zeroPadTime does, just for days.

controlled

Can 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.

intervalDelay

Defaults to 1000ms. With higher [`pre

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScriptcomponentcountdownhacktoberfestreact

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架