
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
use-timers-react
Advanced tools
use-timers-react is a drop-in replacement for traditional JavaScript timing APIs, tailored for React. This library introduces hooks that enhance the implementation of timers, intervals, and timeouts in your React components, ensuring seamless integration and reliability. With automatic cleanup on component unmount, it eliminates the need for manual timing management, making your code cleaner and more efficient.
import {useInterval, useTimeout} from 'use-timers-react'
export default function App () {
const {setTimeout} = useTimeout();
const {setInterval} = useInterval();
function demo(){
setTimeout(() => {
console.log('hello react from timeout');
}, 1000);
setInterval(() => {
console.log('hello react from interval');
}, 1000);
}
return <JSX/>
}
Though automatic timer cleanup on component unmount is handled for you, still if you need to cancel timers conditionally, the following methods are available:
.clear() method (recommended)import {useInterval, useTimeout} from 'use-timers-react'
export default function App () {
const {setTimeout} = useTimeout();
function demo(){
const logTime = () => console.log('hello react: ', Date.now());
let myTimer = setTimeout(logDate, 100);
myTimer.clear();
}
return <JSX/>
}
clearTimeout or clearIntervalimport {useInterval, useTimeout} from 'use-timers-react'
export default function App () {
const {setTimeout, clearTimeout} = useTimeout();
function demo(){
let timeout = setTimeout(() => {
console.log('hello react from timeout');
}, 1000);
clearTimeout(timeout);
}
return <JSX/>
}
clearAllTimeouts or clearAllIntervalsimport {useInterval, useTimeout} from 'use-timers-react'
export default function App () {
const {setTimeout, clearAllTimeouts} = useTimeout();
function set2Timers(){
const logTime = () => console.log('hello react: ', Date.now());
setTimeout(logTime, 1000);
setTimeout(logTime, 2000);
}
return <>
<JSX/>
<button onClick={()=>clearAllTimeouts()}>clear all timeouts</button>
</>
}
keyA key can be specified in two ways:
as string to the last argument
object to the last argument
import {useInterval, useTimeout} from 'use-timers-react'
export default function App () {
const {setTimeout, clearTimeout} = useTimeout();
function set2Timers(){
const logTime = () => console.log('hello react: ', Date.now());
//passing key as string
setTimeout(logTime, 2000, 'timer1');
//passing key using object
setTimeout(logTime, 2000, {key: 'timer2'});
}
function clearTimer(){
clearTimeout('timer1');
clearTimeout('timer2');
}
return <JSX/>
}
.resetimport {useInterval, useTimeout} from 'use-timers-react'
export default function App () {
const {setTimeout} = useTimeout();
function demo(){
const logTime = () => console.log('hello react: ', Date.now());
let myTimer = setTimeout(logDate, 1000);
setTimeout(()=>myTimer.reset(), 500)
}
return <JSX/>
}
onClear and onReset callbacks:import {useInterval, useTimeout} from 'use-timers-react'
export default function App () {
const {setTimeout} = useTimeout();
function demo(){
const logTime = () => console.log('hello react: ', Date.now());
let myTimer = setTimeout(logDate, 1000, {
onClear: () => console.log('cleared'),
onReset: () => console.log('reset'),
});
setTimeout(()=>myTimer.reset(), 500)
}
return <JSX/>
}
FAQs
A drop in replacement for js timeout apis in react components.
We found that use-timers-react demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.