Skip to content

Instantly share code, notes, and snippets.

@jayantbh
Created June 21, 2020 16:10
Show Gist options
  • Save jayantbh/21a2bda776e2036fded54eeb5d6e5162 to your computer and use it in GitHub Desktop.
Save jayantbh/21a2bda776e2036fded54eeb5d6e5162 to your computer and use it in GitHub Desktop.
useFrame hook - takes a function, and throttles it using requestAnimationFrame
/* eslint-env browser */
import {useRef} from 'react';
export const useFrame = <T = any>(fn: (...args: T[]) => any) => {
const frameRef = useRef();
return (..._args: T[]) => {
frameRef.current && cancelAnimationFrame(frameRef.current);
frameRef.current = requestAnimationFrame(() => fn(..._args));
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment