refactor: debounce.jsx to typescript and moving it to Hooks
This commit is contained in:
1 parent
07b0483352
commit
e75606aad3
4 files changed
+31
-20
No files matched your search
@@ -0,0 +1,29 @@
|
||||
import { useState, useEffect } from "react";
|
||||
|
||||
/**
|
||||
* Custom hook for debouncing values
|
||||
* @template T - The type of the value being debounced
|
||||
* @param value - The value to debounce
|
||||
* @param delay - The debounce delay in milliseconds
|
||||
* @returns The debounced value
|
||||
*
|
||||
* @example
|
||||
* const debouncedSearch = useDebounce(search, 300); // Debounces the search input with a 300ms delay
|
||||
*/
|
||||
const useDebounce = <T,>(value: T, delay: number): T => {
|
||||
const [debouncedValue, setDebouncedValue] = useState<T>(value);
|
||||
|
||||
useEffect(() => {
|
||||
const handler = setTimeout(() => {
|
||||
setDebouncedValue(value);
|
||||
}, delay);
|
||||
|
||||
return () => {
|
||||
clearTimeout(handler);
|
||||
};
|
||||
}, [value, delay]);
|
||||
|
||||
return debouncedValue;
|
||||
};
|
||||
|
||||
export default useDebounce;
|
||||
Reference in new issue
Block a user