-
Notifications
You must be signed in to change notification settings - Fork 113
/
useDebounce.ts
28 lines (23 loc) · 987 Bytes
/
useDebounce.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { useCallback, useEffect, useRef, useState } from 'react';
import useDebouncedCallback from './useDebouncedCallback';
function valueEquality<T>(left: T, right: T): boolean {
return left === right;
}
export default function useDebounce<T>(
value: T,
delay: number,
options?: { maxWait?: number; leading?: boolean; equalityFn?: (left: T, right: T) => boolean }
): [T, () => void] {
const eq = options && options.equalityFn ? options.equalityFn : valueEquality;
const [state, dispatch] = useState(value);
const [callback, cancel] = useDebouncedCallback(useCallback((value) => dispatch(value), []), delay, options);
const previousValue = useRef(value);
useEffect(() => {
// We need to use this condition otherwise we will run debounce timer for the first render (including maxWait option)
if (!eq(previousValue.current, value)) {
callback(value);
previousValue.current = value;
}
}, [value, callback, eq]);
return [state, cancel];
}