Socket
Socket
Sign inDemoInstall

react-countdown-hook

Package Overview
Dependencies
3
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-countdown-hook

Dead simple yet powerful countdown hook for React.


Version published
Weekly downloads
7.1K
increased by7.9%
Maintainers
1
Install size
26.8 kB
Created
Weekly downloads
 

Readme

Source

react-countdown-hook

Dead simple yet powerful countdown hook for React. Powered by requestAnimationFrame.

Installation

Using npm:

$ npm install --save react-countdown-hook

Using yarn:

$ yarn add react-countdown-hook

Quick Start

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 or in the 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

Keywords

FAQs

Last updated on 13 Mar 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc