refactor: clean code and remove unnecessary dependencies
This commit is contained in:
1 parent
eceae6fec7
commit
ebf599eed6
92 files changed
+1152
-2842
No files matched your search
@@ -0,0 +1,25 @@
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
|
||||
function useDistinctUntilChanged<T>(value: T, delayMS: number = 500): T {
|
||||
const [distinctValue, setDistinctValue] = useState(value);
|
||||
const previousValueRef = useRef(value);
|
||||
const timeoutRef = useRef<NodeJS.Timeout>();
|
||||
|
||||
useEffect(() => {
|
||||
const clear = () => clearTimeout(timeoutRef.current);
|
||||
|
||||
if (previousValueRef.current === value) return clear;
|
||||
|
||||
previousValueRef.current = value;
|
||||
|
||||
clearTimeout(timeoutRef.current);
|
||||
|
||||
timeoutRef.current = setTimeout(() => setDistinctValue(value), delayMS);
|
||||
|
||||
return clear;
|
||||
}, [value, delayMS]);
|
||||
|
||||
return distinctValue;
|
||||
}
|
||||
|
||||
export default useDistinctUntilChanged;
|
||||
Reference in new issue
Block a user