Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
react-timing-hooks
Advanced tools
React hooks for setTimeout, setInterval, requestAnimationFrame, requestIdleCallback
requestAnimationFrame
setTimeout
setInterval
requestIdleCallback
useTimer
useAnimationFrameLoop
# via npm
npm i react-timing-hooks
# via yarn
yarn add react-timing-hooks
import { useState } from 'react'
import { useAnimationFrameLoop } from 'react-timing-hooks'
const AnimationFrameCounter = ({ depA, depB }) => {
const [count, setCount] = useState(0)
const [stop, setStop] = useState(false)
useAnimationFrameLoop(() => {
setCount(count + 1)
}, stop)
return (
<div>
<p>{count}</p>
<button onClick={() => setStop(!stop)}>
Stop counting
</button>
</div>
)
}
Writing a timeout or anything similar requires a lot of boilerplate (if you don't do it quick and dirty). This library is supposed to give you easy access to those functionalities while keeping your code clean.
For example: You might have a timeout that runs under a certain condition. In this case a cleanup
has to be done in a separate useEffect
call that cleans everything up (but only on unmount).
Your code could look like this:
import { useEffect } from 'react'
const TimeoutRenderer = ({ depA, depB }) => {
const [output, setOutput] = useState(null)
const timeoutId = useRef(null)
useEffect(() => {
if (depA && depB) {
timeoutId.current = setTimeout(() => setOutput('Hello World'), 1000)
}
}, [depA, depB])
useEffect(() => {
return function onUnmount() {
if (timeoutId.current !== null) {
clearTimeout(timeoutId.current)
}
}
}, [timeoutId])
return output ? (
<div>{output}</div>
) : null
}
With react-timing-hooks
you can just write:
import { useState } from 'react'
import { useTimeoutEffect } from 'react-timing-hooks'
const TimeoutRenderer = ({ depA, depB }) => {
const [output, setOutput] = useState(null)
useTimeoutEffect((timeout) => {
if (depA && depB) {
timeout(() => setOutput('Hello World'), 1000)
}
}, [depA, depB])
return output ? (
<div>{output}</div>
) : null
}
In this case react-timing-hooks
automatically took care of cleaning up the timeout for you (if the component is mounted for less than a second for instance).
https://ericlambrecht.github.io/react-timing-hooks/
see CONTRIBUTING.md
1.3.2 (2020-03-28)
Updated homepage
in package.json
and overhauled the documentation.
Technically identical to 1.3.1
.
FAQs
React hooks for setTimeout, setInterval, requestAnimationFrame, requestIdleCallback
The npm package react-timing-hooks receives a total of 829 weekly downloads. As such, react-timing-hooks popularity was classified as not popular.
We found that react-timing-hooks demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.