Installations
npm install react-timer-hook
Developer
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
Yes
Node Version
20.10.0
NPM Version
10.2.3
Statistics
519 Stars
262 Commits
112 Forks
4 Watching
3 Branches
7 Contributors
Updated on 26 Nov 2024
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
9,030,706
Last day
0.8%
19,821
Compared to previous day
Last week
-2.7%
97,653
Compared to previous week
Last month
16.3%
415,850
Compared to previous month
Last year
46.4%
4,112,438
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
1
Dev Dependencies
21
react-timer-hook
React timer hook is a custom react hook, built to handle timer, stopwatch, and time logic/state in your react component.
useTimer
: Timers (countdown timer)useStopwatch
: Stopwatch (count up timer)useTime
: Time (return current time)
Setup
yarn add react-timer-hook
OR npm install --save react-timer-hook
useTimer
- Demo
Example
1import React from 'react'; 2import { useTimer } from 'react-timer-hook'; 3 4function MyTimer({ expiryTimestamp }) { 5 const { 6 totalSeconds, 7 seconds, 8 minutes, 9 hours, 10 days, 11 isRunning, 12 start, 13 pause, 14 resume, 15 restart, 16 } = useTimer({ expiryTimestamp, onExpire: () => console.warn('onExpire called') }); 17 18 19 return ( 20 <div style={{textAlign: 'center'}}> 21 <h1>react-timer-hook </h1> 22 <p>Timer Demo</p> 23 <div style={{fontSize: '100px'}}> 24 <span>{days}</span>:<span>{hours}</span>:<span>{minutes}</span>:<span>{seconds}</span> 25 </div> 26 <p>{isRunning ? 'Running' : 'Not running'}</p> 27 <button onClick={start}>Start</button> 28 <button onClick={pause}>Pause</button> 29 <button onClick={resume}>Resume</button> 30 <button onClick={() => { 31 // Restarts to 5 minutes timer 32 const time = new Date(); 33 time.setSeconds(time.getSeconds() + 300); 34 restart(time) 35 }}>Restart</button> 36 </div> 37 ); 38} 39 40export default function App() { 41 const time = new Date(); 42 time.setSeconds(time.getSeconds() + 600); // 10 minutes timer 43 return ( 44 <div> 45 <MyTimer expiryTimestamp={time} /> 46 </div> 47 ); 48}
Settings
key | Type | Required | Description |
---|---|---|---|
expiryTimestamp | Date object | YES | this will define for how long the timer will be running |
autoStart | boolean | No | flag to decide if timer should start automatically, by default it is set to true |
onExpire | Function | No | callback function to be executed once countdown timer is expired |
Values
key | Type | Description |
---|---|---|
seconds | number | seconds value |
minutes | number | minutes value |
hours | number | hours value |
days | number | days value |
totalSeconds | number | total number of seconds left in timer NOT converted to minutes, hours or days |
isRunning | boolean | flag to indicate if timer is running or not |
pause | function | function to be called to pause timer |
start | function | function if called after pause the timer will continue based on original expiryTimestamp |
resume | function | function if called after pause the timer will continue countdown from last paused state |
restart | function | function to restart timer with new expiryTimestamp, accept 2 arguments first is the new expiryTimestamp of type Date object and second is autoStart of type boolean to decide if it should automatically start after restart or not, default is true |
useStopwatch
- Demo
Example
1import React from 'react'; 2import { useStopwatch } from 'react-timer-hook'; 3 4function MyStopwatch() { 5 const { 6 totalSeconds, 7 seconds, 8 minutes, 9 hours, 10 days, 11 isRunning, 12 start, 13 pause, 14 reset, 15 } = useStopwatch({ autoStart: true }); 16 17 18 return ( 19 <div style={{textAlign: 'center'}}> 20 <h1>react-timer-hook</h1> 21 <p>Stopwatch Demo</p> 22 <div style={{fontSize: '100px'}}> 23 <span>{days}</span>:<span>{hours}</span>:<span>{minutes}</span>:<span>{seconds}</span> 24 </div> 25 <p>{isRunning ? 'Running' : 'Not running'}</p> 26 <button onClick={start}>Start</button> 27 <button onClick={pause}>Pause</button> 28 <button onClick={reset}>Reset</button> 29 </div> 30 ); 31} 32 33export default function App() { 34 return ( 35 <div> 36 <MyStopwatch /> 37 </div> 38 ); 39}
Settings
key | Type | Required | Description |
---|---|---|---|
autoStart | boolean | No | if set to true stopwatch will auto start, by default it is set to false |
offsetTimestamp | Date object | No | this will define the initial stopwatch offset example: const stopwatchOffset = new Date(); stopwatchOffset.setSeconds(stopwatchOffset.getSeconds() + 300); this will result in a 5 minutes offset and stopwatch will start from 0:0:5:0 instead of 0:0:0:0 |
Values
key | Type | Description |
---|---|---|
seconds | number | seconds value |
minutes | number | minutes value |
hours | number | hours value |
days | number | days value |
totalSeconds | number | total number of seconds in stopwatch NOT converted to minutes, hours or days |
isRunning | boolean | flag to indicate if stopwatch is running or not |
start | function | function to be called to start/resume stopwatch |
pause | function | function to be called to pause stopwatch |
reset | function | function to be called to reset stopwatch to 0:0:0:0, you can also pass offset parameter to this function to reset stopwatch with offset, similar to how offsetTimestamp will offset the initial stopwatch time, this function will accept also a second argument which will decide if stopwatch should automatically start after reset or not default is true |
useTime
- Demo
Example
1import React from 'react'; 2import { useTime } from 'react-timer-hook'; 3 4function MyTime() { 5 const { 6 seconds, 7 minutes, 8 hours, 9 ampm, 10 } = useTime({ format: '12-hour'}); 11 12 return ( 13 <div style={{textAlign: 'center'}}> 14 <h1>react-timer-hook </h1> 15 <p>Current Time Demo</p> 16 <div style={{fontSize: '100px'}}> 17 <span>{hours}</span>:<span>{minutes}</span>:<span>{seconds}</span><span>{ampm}</span> 18 </div> 19 </div> 20 ); 21} 22 23export default function App() { 24 return ( 25 <div> 26 <MyTime /> 27 </div> 28 ); 29}
Settings
key | Type | Required | Description |
---|---|---|---|
format | string | No | if set to 12-hour time will be formatted with am/pm |
Values
key | Type | Description |
---|---|---|
seconds | number | seconds value |
minutes | number | minutes value |
hours | number | hours value |
ampm | string | am/pm value if 12-hour format is used |
Deprecation Warning:
Starting from v1.1.0
and above default export useTimer
is deprecated, your old code will still work but it is better to start using named exports { useTimer, useStopwatch, useTime }
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
2 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-c7qv-q95q-8v27
Reason
5 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 4
Reason
Found 2/11 approved changesets -- score normalized to 1
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 22 are checked with a SAST tool
Score
4
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More