Get Shadcn UI Kit -
Orbit Chat
Pinned
Recent
How do I debounce a search input in React without adding a library?

Wrap the value in a tiny hook that waits until the user stops typing. A few things to keep in mind:

  • Clear the previous timer on every keystroke
  • Keep the delay between 250 and 400 ms for search
  • Fetch from an effect that depends on the debounced value
tsx
function useDebounce<T>(value: T, delay = 300) {
  const [debounced, setDebounced] = useState(value);
  useEffect(() => {
    const id = setTimeout(() => setDebounced(value), delay);
    return () => clearTimeout(id);
  }, [value, delay]);
  return debounced;
}

Then call useDebounce(query) and run your request whenever the result changes.

Orbit can make mistakes. Check important info.