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 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-hook-time

v1.0.7-5

Published

A React timer library

Downloads

1,898

Readme

react-hook-time

react-hook-time is a library for React that allows you to create timers and stopwatches in your applications. It supports TypeScript and provides a simple and clear API, making it easy to customize according to your needs. You can set initial time, choose time units, configure callbacks, and much more.

Open basic demo to see how it works

Go to npm page

Install

npm install react-hook-time

Quickstart

import useTimer from 'react-hook-time'

function App() {
  const { currentTime, start, pause, reset } = useTimer(10, {
    onEnd: () => console.log('Timer ended'),
  })

  return (
    <div>
      <div>Current time {currentTime}</div>
      <button onClick={start}>start</button>
      <button onClick={pause}>pause</button>
      <button onClick={() => {
        // chaining functions
        reset().pause()
      }}>
        reset
      </button>
    </div>
  )
}

API

There are few options to initialize timer. You can pass:

  • initialTime
  • initialTime and settings object
  • settings object
  • leave it empty to set it up later with setTime for async setup

initialTime can be number or Date object

const timer = useTimer(10)
const timerFromDate = useTimer(new Date('2023-12-01'))
const timerWithoutUpdates = useTimer(15, { stopUpdate: true })
const stopwatch = useTimer({ stopwatch: true })
const timerWithoutSettings = useTimer()

Props

name | description | type | default value --|--|--|-- autostart | enables autostart on component mount | boolean | false step* | by default tick step is 1000 millisecond (1 sec). But you can change it | number | 1000 timeUnit | indicates the default time unit in which the timer will operate | 'ms' | 'sec' | 'min' | 'hour' | 'day' | 'sec' stopUpdate* | disables component re-render on every tick | boolean | false stopwatch* | enables stopwatch with time going up | boolean | false speedUpFirstSecond* | first tick will happen faster after timer starts. Visual thing similar to iOS timers | boolean | false

speedUpFirstSecond - сompare the two sides. In both cases, the 'start' button was clicked simultaneously. However, on the right side, the timer visually starts faster.

speedUpFirstSecond

step

step

stopUpdate - with this prop most of the callbacks are not working. Only onStart, onEnd and onCancel are available. currentTime and formattedCurrentTime is not available. Use getCurrentTime() and getFormattedCurrentTime() instead.

stopwatch - with this prop onEnd callback is disabled

Methods

const timer = useTimer(10)

timer returns some values and functions. You can use them separately timer.start() or chain them if required timer.reset().pause() name | description | type --|--|-- currentTime | current time | number getCurrentTime | same as currentTime but for stopUpdate=true | number formattedCurrentTime | you can get years, days, hours, minutes, seconds from this object | object getFormattedCurrentTime | same as formattedCurrentTime but for stopUpdate=true | object isRunning | current timer state | boolean start | start timer | () => void pause | pause timer | () => void reset | reset time to initial value | () => void setStep | set new step in milliseconds | (number) => void setTime | set new time value | (timeAmount, timeSettings) => void decTime | decrease time | (timeAmount, timeSettings) => void incTime | increase time | (timeAmount, timeSettings) => void

timeAmount

timeAmount can be number or Date object

timeSettings

name | description | type | default value --|--|--|-- timeUnit | specifying the time unit to perform a function | 'ms' | 'sec' | 'min' | 'hour' | 'day' | 'sec' or timeUnit used in useTimer props

Callbacks

name | description | return value --|--|-- onCancel | triggers when timer was cancelled | undefined onEnd | triggers when timer was ended | undefined onPause | triggers when timer was paused | currentTime onStart | triggers when timer was started | currentTime onReset | triggers when timer was reseted | currentTime onUpdate | triggers on every tick | currentTime onTimeSet | triggers when timer was set | currentTime onTimeInc | triggers when timer was increased | currentTime onTimeDec | triggers when timer was decreased | currentTime onStepSet | triggers when step was set | step