# react-countdown-hook

> Dead simple yet powerful countdown hook for React.

Latest version **1.1.3** (published 2023-03-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-countdown-hook
pnpm add react-countdown-hook
yarn add react-countdown-hook
bun add react-countdown-hook
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.3 |
| Published | 2023-03-13 |
| First published | 2019-06-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 26.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 75 |
| Author | Alex Khismatulin |
| Maintainers | alexkhismatulin |
| Keywords | countdown, hook, hooks, timer, react, react-hooks, react-use-countdown |

## Links

- npm: https://www.npmjs.com/package/react-countdown-hook
- Repository: https://github.com/alexkhismatulin/react-use-count-down
- Homepage: https://github.com/alexkhismatulin/react-use-count-down#readme
- Issues: https://github.com/alexkhismatulin/react-use-count-down/issues
- npm.io page: https://npm.io/package/react-countdown-hook

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.1.3 (latest) — 2023-03-13
- 1.1.1 — 2021-10-26
- 1.1.0 — 2021-04-05
- 1.0.3 — 2021-01-17
- 1.0.2 — 2020-12-20
- 1.0.1 — 2020-08-16
- 1.0.0 — 2020-08-16
- 0.2.0 — 2020-05-26
- 0.1.1 — 2019-06-02
- 0.1.0 — 2019-06-02

## README

![react-countdown-hook](./assets/cover.png)
<p align="center">Dead simple yet powerful countdown hook for React. Powered by <code>requestAnimationFrame</code>.</p>
<p align="center">
    <a href="https://circleci.com/gh/alexkhismatulin/react-use-count-down">
        <img src="https://circleci.com/gh/alexkhismatulin/react-use-count-down.svg?style=svg" />
    </a>
    <a href="https://coveralls.io/github/alexkhismatulin/react-use-count-down?branch=master">
        <img src="https://coveralls.io/repos/github/alexkhismatulin/react-use-count-down/badge.svg?branch=master" />
    </a>
    <a href="https://www.npmjs.com/package/react-countdown-hook">
        <img src="https://img.shields.io/npm/v/react-countdown-hook.svg" />
    </a>
</p>

## Installation
Using npm:

```sh
$ npm install --save react-countdown-hook
```

Using yarn:

```sh
$ yarn add react-countdown-hook
```

## Quick Start
```javascript
import React from 'react';
import useCountDown from 'react-countdown-hook';

const initialTime = 60 * 1000; // initial time in milliseconds, defaults to 60000
const interval = 1000; // interval to change remaining time amount, defaults to 1000

const render = () => {
  const [timeLeft, { start, pause, resume, reset }] = useCountDown(initialTime, interval);
  
  // start the timer during the first render
  React.useEffect(() => {
    start();
  }, []);
  
  const restart = React.useCallback(() => {
    // you can start existing timer with an arbitrary value
    // if new value is not passed timer will start with initial value
    const newTime = 42 * 1000;
    start(newTime);
  }, []);
 
  return (
    <>
      <p>Time left: {timeLeft}</p>
 
      <button onClick={restart}>
        Restart counter with 42 seconds
      </button>
    </>
  );
}
```

Note that this is a very basic usage. Check out more usage examples
[in playground](https://stackblitz.com/edit/react-use-count-down?file=index.js "react-countdown-hook on stackblitz")
or [in the demo project](./demo/index.js "react-countdown-hook demo project")

## Documentation
### [timeLeft, actions] = useCountDown(timeToCount, interval)

#### Parameters
Takes a default countdown time and interval time.
* `timeToCount` {`Number`} Time in milliseconds that countdown should start with. **Defaults to `60000`**
* `interval` {`Number`} Time in milliseconds representing the frequency that countdown should update with. **Defaults to `1000`**

#### Return value
Returns an array with remaining time and actions object.
* `timeLeft` {`Number`} Remaining countdown time in milliseconds
* `actions.start` {`Function`} Start or restart a countdown. Takes time in milliseconds to start with.
* `actions.reset` {`Function`} Resets a countdown to initial state
* `actions.pause` {`Function`} Pauses a countdown
* `actions.resume` {`Function`} Resumes a paused countdown

## Contributing
Feel free to submit any issues or pull requests.

## License
MIT

---
_Source: https://npm.io/package/react-countdown-hook · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
