diff --git a/src/components/SearchBar.tsx b/src/components/SearchBar.tsx index 80bf583..2667a55 100644 --- a/src/components/SearchBar.tsx +++ b/src/components/SearchBar.tsx @@ -25,6 +25,16 @@ type SearchBarProps = { onKeyDown?: (e: KeyboardEvent) => void; }; +const debounce = (func: (...args: any[]) => void, delay: number) => { + let timeoutId: NodeJS.Timeout; + return (...args: any[]) => { + clearTimeout(timeoutId); + timeoutId = setTimeout(() => { + func(...args); + }, delay); + }; +}; + export const SearchBar = ({ placeholder, value, @@ -40,14 +50,11 @@ export const SearchBar = ({ setInput(e.target.value); }; + const debouncedSetSearch = debounce(setSearch, 300); + useEffect(() => { - const handler = setTimeout(() => { - setSearch(input); - }, 300); - return () => { - clearTimeout(handler); - }; - }, [input, setSearch]); + debouncedSetSearch(input); + }, [input, debouncedSetSearch]); return (