Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-hook-time

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-hook-time

A React timer library

  • 1.0.7-5
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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
namedescriptiontypedefault value
autostartenables autostart on component mountbooleanfalse
step*by default tick step is 1000 millisecond (1 sec). But you can change itnumber1000
timeUnitindicates the default time unit in which the timer will operate'ms' | 'sec' | 'min' | 'hour' | 'day''sec'
stopUpdate*disables component re-render on every tickbooleanfalse
stopwatch*enables stopwatch with time going upbooleanfalse
speedUpFirstSecond*first tick will happen faster after timer starts. Visual thing similar to iOS timersbooleanfalse

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()

namedescriptiontype
currentTimecurrent timenumber
getCurrentTimesame as currentTime but for stopUpdate=truenumber
formattedCurrentTimeyou can get years, days, hours, minutes, seconds from this objectobject
getFormattedCurrentTimesame as formattedCurrentTime but for stopUpdate=trueobject
isRunningcurrent timer stateboolean
startstart timer() => void
pausepause timer() => void
resetreset time to initial value() => void
setStepset new step in milliseconds(number) => void
setTimeset new time value(timeAmount, timeSettings) => void
decTimedecrease time(timeAmount, timeSettings) => void
incTimeincrease time(timeAmount, timeSettings) => void
timeAmount

timeAmount can be number or Date object

timeSettings
namedescriptiontypedefault value
timeUnitspecifying the time unit to perform a function'ms' | 'sec' | 'min' | 'hour' | 'day''sec' or timeUnit used in useTimer props
Callbacks
namedescriptionreturn value
onCanceltriggers when timer was cancelledundefined
onEndtriggers when timer was endedundefined
onPausetriggers when timer was pausedcurrentTime
onStarttriggers when timer was startedcurrentTime
onResettriggers when timer was resetedcurrentTime
onUpdatetriggers on every tickcurrentTime
onTimeSettriggers when timer was setcurrentTime
onTimeInctriggers when timer was increasedcurrentTime
onTimeDectriggers when timer was decreasedcurrentTime
onStepSettriggers when step was setstep

Keywords

FAQs

Package last updated on 12 Nov 2023

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc