I have a custom useAnimationFrame Hook:
import React, { useRef } from 'react';
const useAnimationFrame = (callback: (arg0: number) => void) => {
// Use useRef for mutable variables that we want to persist
// without triggering a re-render on their change
const requestRef = useRef<number>();
const previousTimeRef = useRef<number>();
const animate = (time: number) => {
if (previousTimeRef.current) {
const deltaTime = time - previousTimeRef.current;
callback(deltaTime);
}
previousTimeRef.current = time;
requestRef.current = requestAnimationFrame(animate);
};
React.useEffect(() => {
requestRef.current = requestAnimationFrame(animate);
return () => {
if (requestRef.current) {
cancelAnimationFrame(requestRef.current);
}
};
}, []); // Make sure the effect runs only once
};
export default useAnimationFrame;
And I would like to use it this way, to update what is happening with each frame:
const exerciseRunning = useRef(true);
const directionForward = useRef(true);
const [progress, setProgress] = useState(0);
const [coord1, setCoord1] = useState({x: 0, y: 0});
const [coord2, setCoord2] = useState({x: 0, y: 0});
const [percentage, setPercentage] = useState(1);
useAnimationFrame((deltaTime) => {
// Works!
if (!exerciseRunning.current) {
return;
}
// setProgress works!
// directon.Forward works!
// percentage does not work!
setProgress((prevState) =>
directionForward.current
? prevState + percentage * 1
: prevState - percentage * 2
);
});
The references exerciseRunning and directionForward both work properly and so does the setProgress.
I know, that the matching() and percentage parts are set at the first render (for the inside of useAnimationFrame) and do not change afterward. How do I need to change the construct, so that I can use the updated state variables?