How would you build a search with autocomplete in React?
Implement a fully working autocomplete component with debouncing, keyboard navigation, async suggestions, loading states, and accessibility.
advancedUi patternsreactautocompletesearchdebounce
Published: 11/19/2025
Updated: 11/19/2025
Advertisement
đź§© Scenario
Your product team wants a Google-like autocomplete search bar. Requirements:
- Fetch suggestions from an API with debouncing
- Show a dropdown with highlighted items
- Keyboard navigation (↑ ↓ Enter)
- Click selection
- Loading state + empty state
- Close dropdown on blur
- Accessible via ARIA roles
Build a reusable autocomplete component.
đź§ Answer (Design Overview)
Core Architectural Points:
- Debounced Input — delay API calls by 300ms.
- Async Suggestion Fetcher — separate fetch logic for reuse.
- Dropdown State Machine — open/close on input, blur, and selection.
- Keyboard Navigation — maintain
activeIndexin state. - High-performance Rendering — memoize list items.
- Accessibility — use
role="listbox"androle="option".
Tradeoffs:
- Controlled inputs are predictable but more verbose.
- Debouncing improves performance but delays response slightly.
🎮 Live Demo: Search Autocomplete
🔍 Real-World Notes
- Use AbortController to prevent race conditions.
- For large datasets, move filtering to backend.
- Add caching to prevent re-searching the same query.
- For best UX, preload common queries (trending, recent).
đź§ Diagram
sequenceDiagram participant User participant Input participant Debounce participant API User->>Input: type text Input->>Debounce: wait 300ms Debounce->>API: fetch suggestions API-->>Debounce: return list Debounce-->>Input: update dropdown
Quick Practice
- Add arrow-key wraparound behavior.
- Add highlighting of matched characters.
- Add caching layer:
{ [query]: results }.
Summary
- Debounce input to avoid spammy requests.
- Manage dropdown visibility explicitly.
- Add keyboard navigation for accessibility.
- Abort in-flight requests to avoid stale updates.
Frequently Asked Questions
Why use debouncing?
Debouncing prevents unnecessary API calls by waiting for the user to finish typing.
How do you support keyboard navigation?
Track active index and respond to ArrowUp/ArrowDown/Enter keys.
Advertisement
Stay Updated
Get the latest frontend challenges, interview questions and tutorials delivered to your inbox.
Advertisement