New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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

  • 0.0.1-beta.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
274
decreased by-18.93%
Maintainers
1
Weekly downloads
 
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.

Demo

Install

npm install react-hook-time

Quickstart

import { useTimer } from 'react-hook-time'

function App() {
  const { currentTime, start, pause } = useTimer(10)

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

API

There are 3 options to pass arguments to useTimer(). You can pass initialTime, initialTime and settings object or just settings object

const timer = useTimer(10)
const timerWithoutUpdates = useTimer(15, { preventUpdate: true })
const stopwatch = useTimer({ stapwatch: true })
Props
namedescriptiontypedefault value
autostartenables autostart on component mountbooleanfalse
preventUpdate*disables component re-render on every tickbooleanfalse
stopwatch*enables stopwatch with time going upbooleanfalse
speedUpFirstSecondfirst tick will happen faster after timer starts. Visual thing similar to iOS timersbooleanfalse

preventUpdate - with this prop most of the callbacks are not working. Only onStart, onEnd and onCancel are available stopwatch - with this prop onEnd callback is disabled

Methods

const timer = useTimer(10)

timer returns some values and functions

namedescriptiontype
currentTimecurrent timenumber
formattedCurrentTimeyou can get years, days, hours, minutes, seconds from this objectobject
isRunningcurrent timer stateboolean
startstart timer() => void
pausepause timer() => void
resetreset time to initial value(resetSettings) => void
setTimeset new time value(timeAmount, timeSettings) => void
decTimeBydecrease time(timeAmount, timeSettings) => void
incTimeByincrease time(timeAmount, timeSettings) => void
resetSettings
namedescriptiontypedefault value
startIfWasStoppedstarts timer after updating, increasing and decreasing time if was stopped beforebooleanfalse
continueIfWasRunningcontinues timer after updating, increasing and decreasing time if was running beforebooleanfalse
timeSettings

The same as resetSettings with one more key

namedescriptiontypedefault value
timeUnitspecifying the time unit to perform a function'ms' | 'sec' | 'min' | 'hour' | 'day''sec'
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

FAQs

Package last updated on 30 Sep 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