mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
36 lines
978 B
TypeScript
36 lines
978 B
TypeScript
/* eslint-disable */
|
|
import { useEffect, useRef, useState } from 'react';
|
|
import useUnmount from './useUnmount';
|
|
|
|
const useThrottleFn = <T, U extends any[]>(fn: (...args: U) => T, ms: number = 200, args: U) => {
|
|
const [state, setState] = useState<T | null>(null);
|
|
const timeout = useRef<ReturnType<typeof setTimeout>>();
|
|
const nextArgs = useRef<U>();
|
|
|
|
useEffect(() => {
|
|
if (!timeout.current) {
|
|
setState(fn(...args));
|
|
const timeoutCallback = () => {
|
|
if (nextArgs.current) {
|
|
setState(fn(...nextArgs.current));
|
|
nextArgs.current = undefined;
|
|
timeout.current = setTimeout(timeoutCallback, ms);
|
|
} else {
|
|
timeout.current = undefined;
|
|
}
|
|
};
|
|
timeout.current = setTimeout(timeoutCallback, ms);
|
|
} else {
|
|
nextArgs.current = args;
|
|
}
|
|
}, args);
|
|
|
|
useUnmount(() => {
|
|
timeout.current && clearTimeout(timeout.current);
|
|
});
|
|
|
|
return state;
|
|
};
|
|
|
|
export default useThrottleFn;
|