Debounced search with controlled inputs

839
0

Search inputs that fire API requests on every keystroke create poor UX and waste server resources. Debouncing delays the search until typing pauses, reducing requests dramatically. I combine a controlled input component with a custom useDebounce hook that returns the debounced value. When the debounced value changes, React Query triggers a new search request. The input feels instantly responsive because it's controlled, while the search happens asynchronously after the debounce delay. I set the delay to 300-500ms based on input type—longer for expensive searches, shorter for simple filters. This pattern is essential for autocomplete, live search, and any input-driven queries.