Runs an animating function, calling it before every repaint.
useRef()
hook to create two variables. requestRef
will hold the last request id and previousTimeRef
will hold the last timestamp.animate
, which handles updating these variables, runs the callback
and calls Window.requestAnimationFrame()
perpetually.useEffect()
hook with an empty array to initialize the value of requestRef
using Window.requestAnimationFrame()
. Use the returned value and Window.cancelAnimationFrame()
to clean up when the component unmounts.const useRequestAnimationFrame = callback => {
const requestRef = React.useRef();
const previousTimeRef = React.useRef();
const animate = time => {
if (previousTimeRef.current) callback(time - previousTimeRef.current);
previousTimeRef.current = time;
requestRef.current = requestAnimationFrame(animate);
};
React.useEffect(() => {
requestRef.current = requestAnimationFrame(animate);
return () => cancelAnimationFrame(requestRef.current);
}, []);
};
const Counter = () => {
const [count, setCount] = React.useState(0);
useRequestAnimationFrame(deltaTime => {
setCount(prevCount => (prevCount + deltaTime * 0.01) % 100);
});
return <p>{Math.round(count)}</p>;
};
ReactDOM.render(<Counter />, document.getElementById('root'));
Would you like to help us improve 30 seconds of code?Take a quick survey
React, Hooks
Executes a callback immediately before a component is unmounted and destroyed.
React, Hooks
Runs a callback at most once when a condition becomes true.
React, Hooks
Returns a stateful value, persisted in localStorage
, and a function to update it.