mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
37 lines
990 B
TypeScript
37 lines
990 B
TypeScript
import { useEffect, useRef, useState } from 'react';
|
|
import useUnmount from './useUnmount';
|
|
|
|
const useThrottle = <T>(value: T, ms: number = 200) => {
|
|
const [state, setState] = useState<T>(value);
|
|
const timeout = useRef<ReturnType<typeof setTimeout>>();
|
|
const nextValue = useRef(null) as any;
|
|
const hasNextValue = useRef(0) as any;
|
|
|
|
useEffect(() => {
|
|
if (!timeout.current) {
|
|
setState(value);
|
|
const timeoutCallback = () => {
|
|
if (hasNextValue.current) {
|
|
hasNextValue.current = false;
|
|
setState(nextValue.current);
|
|
timeout.current = setTimeout(timeoutCallback, ms);
|
|
} else {
|
|
timeout.current = undefined;
|
|
}
|
|
};
|
|
timeout.current = setTimeout(timeoutCallback, ms);
|
|
} else {
|
|
nextValue.current = value;
|
|
hasNextValue.current = true;
|
|
}
|
|
}, [value]);
|
|
|
|
useUnmount(() => {
|
|
timeout.current && clearTimeout(timeout.current);
|
|
});
|
|
|
|
return state;
|
|
};
|
|
|
|
export default useThrottle;
|