Skip to content

Latest commit

 

History

History
35 lines (28 loc) · 645 Bytes

README.md

File metadata and controls

35 lines (28 loc) · 645 Bytes

Search with Debounce

Functionality

const filterBySearch = (value: string) => {
  console.log(value);
};

const debounce = (fn: (value: string) => void, delay: number) => {
  let timeoutId: NodeJS.Timeout | number | undefined;

  return (e: FormEvent) => {
    if (timeoutId) {
      clearTimeout(timeoutId);
    }

    timeoutId = setTimeout(() => {
      fn((e.target as HTMLInputElement).value);
    }, delay);
  };
};

Input Section

<input
  className="search-input"
  type="search"
  name="search"
  placeholder="Search"
  onChange={debounce((value) => filterBySearch(value), 500)}
/>