chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import { useEffect, useRef } from "react";
|
||||
|
||||
export function useThrottle(fn: (...args: any[]) => void, duration: number) {
|
||||
const timeout = useRef<ReturnType<typeof setTimeout>>();
|
||||
|
||||
// Clean up when the component is unmounted
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (timeout.current) clearTimeout(timeout.current);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (...args: Parameters<typeof fn>) => {
|
||||
if (timeout.current) {
|
||||
clearTimeout(timeout.current);
|
||||
}
|
||||
|
||||
timeout.current = setTimeout(() => {
|
||||
fn(...args);
|
||||
timeout.current = undefined;
|
||||
}, duration);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user