Skip to content

Commit

Permalink
less debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanjoelson committed Dec 3, 2024
1 parent 083ce21 commit a0b74f6
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/components/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ type SearchBarProps<T extends SearchElement> = {
onKeyDown?: (e: KeyboardEvent<HTMLInputElement>) => 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 = <T extends SearchElement>({
placeholder,
value,
Expand All @@ -40,14 +50,11 @@ export const SearchBar = <T extends SearchElement>({
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 (
<div className="mr-3 w-full max-w-xs md:max-w-sm lg:max-w-md">
Expand Down

0 comments on commit a0b74f6

Please sign in to comment.