Overview
As a leading e-commerce and financial technology company, Paytm handles millions of user interactions daily. A key feature in our product discovery is the search functionality. To provide a smooth user experience while efficiently managing backend resources, it's crucial to implement a search feature that is responsive but doesn't spam our servers with every single keystroke. This problem challenges you to build a debounced search component for a hypothetical Paytm Mall product catalog.
What you need to do / Task
Your goal is to build a React component that implements a debounced search functionality.
- Search Input: Create an input field where users can type their search queries.
- Debouncing Logic: Implement debouncing such that the
fetchProducts API call is only made after a specified delay (e.g., 300ms) of user inactivity. Rapid consecutive typing should only trigger the API call once, after the user pauses.
- Display Results: Show the fetched product names in a list below the search input.
- Loading State: Display a "Loading..." message while the API call is in progress.
- No Results State: Display a "No results found." message if the API returns an empty list for a given search term.
UI/UX expectations
- The component should consist of a search
input element and a div or ul to display the search results.
- When the user types, the input value should update immediately.
- A "Loading..." indicator should appear below the input field while the simulated API call is ongoing.
- Search results should be displayed as a simple list of product names.
- If no results are found, a "No results found." message should be shown.
- Styling can be minimal; focus on functionality and clear presentation of states.
Acceptance criteria
- Typing a character in the search input does not immediately trigger the
fetchProducts mock API.
- An API call is initiated only after 300ms of no further input from the user.
- If the user types "apple" character by character rapidly, only one API call for "apple" should be made after they stop typing for 300ms.
- The component correctly displays product names returned by the mock API.
- A "Loading..." message is visible between the time a debounced API call is triggered and when its results are received.
- If the API returns an empty array, "No results found." is displayed.
- Clearing the input field should clear the results and stop any pending API calls.