My first instinct was to reach for fetch inside an async function and I did that fine, but I fumbled the button disable logic.
Start by outlining the component structure: a button, a results container, and state variables for loading, error, and pagination. Then walk through the implementation step-by-step, emphasizing async/await, error handling, and disabling the button during fetch. Finally, discuss how to detect the end of data and clean up state.
Pro tip: Mention that you'd use a flag like `isLoading` to prevent duplicate requests and that you'd handle race conditions by ignoring stale responses if the user clicks rapidly.
Create a button and a container for results. Initialize state variables: `isLoading`, `error`, `nextPage` (or cursor), and `hasMore`.
Write an async function that fetches data from the endpoint using the current page/cursor. Use try/catch to handle errors and update state accordingly.
Disable the button and show a loading indicator while fetching. On error, display an error message and re-enable the button for retry.
On success, append new items to the container and update the page/cursor. If the server indicates no more data, hide the button or show a message.
Prevent duplicate requests by checking `isLoading`. Consider aborting previous requests if needed. Ensure the button is re-enabled after completion.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.