Skip to content

Repository files navigation

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

importuseTimerfrom'react-hook-time'functionApp(){const{ currentTime, start, pause, reset }=useTimer(10,{onEnd: ()=>console.log('Timer ended'),})return(<div><div>Current time {currentTime}</div><buttononClick={start}>start</button><buttononClick={pause}>pause</button><buttononClick={()=>{// chaining functionsreset().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

consttimer=useTimer(10)consttimerFromDate=useTimer(newDate('2023-12-01'))consttimerWithoutUpdates=useTimer(15,{stopUpdate: true})conststopwatch=useTimer({stopwatch: true})consttimerWithoutSettings=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

Releases

Packages

Used by

Contributors

Languages