React hook for requestAnimationFrame
2022-02-26T13:32:19.224Z
const useAnimationFrame = callback => {
// Use useRef for mutable variables that we want to persist
// without triggering a re-render on their change
const requestRef = React.useRef();
const previousTimeRef = React.useRef();
const animate = time => {
if (previousTimeRef.current != undefined) {
const deltaTime = time - previousTimeRef.current;
callback(deltaTime)
}
previousTimeRef.current = time;
requestRef.current = requestAnimationFrame(animate);
}
React.useEffect(() => {
requestRef.current = requestAnimationFrame(animate);
return () => cancelAnimationFrame(requestRef.current);
}, []); // Make sure the effect runs only once
}
const Counter = () => {
const [count, setCount] = React.useState(0)
useAnimationFrame(deltaTime => {
// Pass on a function to the setter of the state
// to make sure we always have the latest state
setCount(prevCount => (prevCount + deltaTime * 0.01) % 100)
})
return <div>{Math.round(count)}</div>
}
Source : https://css-tricks.com/using-requestanimationframe-with-react-hooks/