React Throttle from https://github.com/streamich/react-use/blob/master/src/useThrottleFn.ts
Saving the reference in case the lib is not maintained anymore
React Throttle from https://github.com/streamich/react-use/blob/master/src/useThrottleFn.ts
Saving the reference in case the lib is not maintained anymore
| 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; | |
| } | |
| return () => timeout.current && clearTimeout(timeout.current); | |
| }, args); | |
| return state; | |
| }; | |
| export default useThrottleFn; |