1 year ago

#367510

test-img

Christian Cascone

useRef variable inside useEffect with requestAnimationFrame and cancelAnimationFrame

in my react native app I run a loop of animations with:

let rafId = useRef(null);

const loop () => {
      ...do something...
};
    
rafId.current = requestAnimationFrame(loop);

In my useEffect when the component will be unmounted I want to stop the last animation.

Is it correct like this?

  useEffect(() => {
    return () => {
      if (rafId.current != null && rafId.current !== 0) 
      {
        cancelAnimationFrame(rafId.current); 
      }
    };
  }, []); 

Thanks

react-native

use-effect

use-ref

0 Answers

Your Answer

Accepted video resources