Wizard V2: Packages refactor, recommendations fix.
This commit is contained in:
parent
8271e6d159
commit
f9aae48dd1
10 changed files with 408 additions and 236 deletions
32
src/Utilities/useDebounce.tsx
Normal file
32
src/Utilities/useDebounce.tsx
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import { useEffect, useState } from 'react';
|
||||
|
||||
import { isEqual } from 'lodash';
|
||||
|
||||
import { DEBOUNCED_SEARCH_WAIT_TIME } from '../constants';
|
||||
|
||||
function useDebounce<T>(
|
||||
value: T,
|
||||
delay: number = DEBOUNCED_SEARCH_WAIT_TIME
|
||||
): T {
|
||||
const [debouncedValue, setDebouncedValue] = useState<T>(value);
|
||||
|
||||
useEffect(() => {
|
||||
// We need to make sure that we compare-deep here as the default useEffect deps do not.
|
||||
if (!isEqual(value, debouncedValue)) {
|
||||
const timer = setTimeout(
|
||||
() => setDebouncedValue(value),
|
||||
value === '' ? 0 : delay !== undefined ? delay : 500 //If value is empty string, instantly return
|
||||
);
|
||||
|
||||
return () => {
|
||||
clearTimeout(timer);
|
||||
};
|
||||
}
|
||||
// Eslint apparrently likes cyclic dependencies.
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [value, delay]);
|
||||
|
||||
return debouncedValue;
|
||||
}
|
||||
|
||||
export default useDebounce;
|
||||
Loading…
Add table
Add a link
Reference in a new issue