1.2.3 • Published 1 year ago

@mzaleski/use-timer v1.2.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

use-timer

React hook that returns a timer's remaining span as a formatted time string, e.g. 65 -> "01:05", as well as numerically.

Table of Contents


Installation

Install through your package manager of choice (npm, yarn, etc.)

npm -i @mzaleski/use-timer
yarn add @mzaleski/use-timer

Usage

import { useTimer } from '@mzaleski/use-timer';

function Component(props) {
  const { timeRemaining, secondsRemaining, setFreeze, resetTimer } = useTimer(65, false,                               
    () => console.log('Timer finished!') 
  );

  return (
    <div>
      <p>Time remaining: {timeRemaining}</p>
      <p>Seconds remaining: {secondsRemaining}</p>
      <button onClick={() => setFreeze(true)}>Freeze</button>
      <button onClick={() => setFreeze(false)}>Unfreeze</button>
      <button onClick={() => resetTimer()}>Reset</button>
    </div>
  );
}

Configuration

The hook's configuration options are as follows:

NameTypeDescription
initialSecondsNumberInitial (remaining) time in seconds
initialFreezeBooleanInitial freeze state
onCompletedFunctionA hook called once the timer has completed

Properties

The hook returns the following properties:

NameTypeDescription
timeRemainingStringThe time remaining as a formatted string, e.g. "01:05"
secondsRemainingNumberThe time remaining as seconds
isFrozenBooleanWhether the timer is frozen
setFreezeFunctionA setter for the timer's freeze state
resetTimerFunctionA function to force-reset the timer – takes the freeze parameter which dictates whether to start the timer upon reset.

TypeScript Support

You will find a collection of typings bundled with the package.

License

MIT License (c) 2022 Maximilien Zaleski

1.2.3

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.2.0

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago