npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-use-timer-hook

v0.2.1

Published

A React hook for managing countdown or count-up timers with pause, reset, and customizable callbacks.

Readme

React Use Timer Hook

GitHub License

A flexible and easy-to-use React custom hook for timer functionality, supporting countdown and count-up modes, pause/resume, and detailed time tracking. Perfect for building timers, countdowns, stopwatches, and progress indicators in your React apps.

Features

  • Countdown & Count Up: Switch between decrementing and incrementing timer modes.
  • Pause/Resume: Pause the timer and track total pause duration.
  • Granular Time Breakdown: Get hours, minutes, seconds, and milliseconds.
  • Reducer-based State: Predictable state management using a reducer pattern.
  • Custom Callbacks: Hooks for onTick, onFinish, and onReset events.
  • TypeScript Support: Fully typed API for safety and autocompletion.

Installation

npm install react-use-timer-hook
# or
yarn add react-use-timer-hook
# or
pnpm add react-use-timer-hook

Usage

import useTimer from 'react-use-timer-hook';

function TimerComponent() {
  const {
    hours,
    minutes,
    seconds,
    milliseconds,
    isRunning,
    isPaused,
    start,
    pause,
    reset,
    isFinished,
  } = useTimer({
    initialTime: 60, // seconds
    countUp: false,  // countdown mode
    onFinish: () => alert('Time is up!'),
    onStart: () => console.log('Started!'),
    onPause: () => console.log('Paused!'),
  });

  return (
    <div>
      <h1>{`${hours}:${minutes}:${seconds}`}</h1>
      <button onClick={start}>Start</button>
      <button onClick={pause}>Pause</button>
      <button onClick={reset}>Reset</button>
  <div>{isRunning ? 'Running' : isPaused ? 'Paused' : isFinished ? 'Finished' : 'Stopped'}</div>
    </div>
  );
}

API

useTimer(options)

Options

| Name | Type | Default | Description | |--------------|-----------|---------|--------------------------------------------------| | time | number | 60 | Initial time in seconds | | countUp | boolean | false | If true, timer counts up instead of down | | autoStart | boolean | false | Start timer automatically | | onFinish | function | - | Callback when timer finishes | | onTick | function | - | Callback on every tick | | onReset | function | - | Callback when timer resets | | onPause | function | - | Callback when timer is paused | | onStart | function | - | Callback when timer is started |

Return Values

| Name | Type | Description | |-------------------|----------|---------------------------------------------| | hours | number | Current hours | | minutes | number | Current minutes | | seconds | number | Current seconds | | milliseconds | number | Current milliseconds | | totalSeconds | number | Total seconds remaining or elapsed | | totalMilliseconds | number | Total milliseconds remaining or elapsed | | isRunning | boolean | Timer is running | | isPaused | boolean | Timer is paused | | isFinished | boolean | Timer has finished | | pauseTime | object | Current pause session duration | | totalPauseTime | object | Total pause time | | start | function | Start or resume the timer | | pause | function | Pause the timer | | reset | function | Reset the timer to initial state | | setTime | function | Set the timer to a specific time (seconds) |

Advanced Usage

  • Count Up Mode: Set countUp: true.
  • Custom Time Display: Use hours, minutes, seconds, or totalSeconds for progress bars.
  • Pause Tracking: Access pauseTime and totalPauseTime for analytics or UI.
  • Detect Finished State: Use isFinished to know when the timer has completed.
  • Lifecycle Callbacks: Use onStart and onPause for custom logic when timer starts or pauses.

Example

const timer = useTimer({
  time: 20,
  countUp: true,
  autoStart: true,
  onFinish: () => console.log('Done!'),
});

Development

  • Built with React hooks and TypeScript
  • Uses dayjs for time calculations
  • Reducer pattern for state management

License

This project is licensed under the MIT License. See the LICENSE file for details.