0.0.0-beta-20230917030612 • Published 3 years ago

@raddix/use-count-down v0.0.0-beta-20230917030612

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Install

npm i @raddix/use-count-down
# or
yarn add @raddix/use-count-down
# or
pnpm add @raddix/use-count-down

Quick Start

import { useCountDown } from '@raddix/use-count-down';

// Using it in a basic way
const Component = () => {
  const [count, { reset }] = useCountDown(10000);

  return (
    <div>
      <span>{Math.round(count / 1000)}</span>
      <button onClick={() => reset()}>Reset</button>
    </div>
  );
};

// Using it in an advanced way, changing its settings
const Component = () => {
  const [count, { start, stop, reset }] = useCountDown(12000, {
    interval: 500,
    autoStart: false
  });

  return (
    <div>
      <span>{Math.round(count / 1000)}</span>
      <button onClick={() => start()}>Start</button>
      <button onClick={() => stop()}>Stop</button>
      <button onClick={() => reset()}>Reset</button>
    </div>
  );
};

API

Parameters

ArgumentTypeRequiredDescription
initialTimenumberYesTime in milliseconds that countdown should start with
optionsOptionsNoA configuration object with the following options below

Options | Argument | Type | Required | Description | | -------- | -------- | --------- | ------------------------------------------------------------------------------------ | | interval | number | No | The time, in milliseconds, that the timer should count down. | | autoStart | boolean | No | Start timer immediately | | onFinished | () => void | No | A callback function to be called when the countdown reaches zero. | | onTick | () => void | No | A callback function to be called on each specified interval of the countdown. |

Returns

The useCountDown hook returns an array with two elements:

IndexTypeParametersDescription
[0]numberThe current count of the countdown.
[1].startfunction(time?: number)Start and resume the countdown, also restart from the time (in milliseconds) indicated in the parameter.
[1].resetfunctionResets the countdown to its initial setting.
[1].stopfunctionPause the countdown.