Implementing Asynchronous Data Fetching in Vue 3 Composition API
This challenge focuses on leveraging the power of the Composition API in Vue 3 to gracefully handle asynchronous operations within the setup function. You will build a component that fetches data from an external API when it's initialized and displays it to the user, while also providing a clear loading state and handling potential errors. This is a fundamental pattern for building modern, interactive web applications.
Problem Description
You need to create a Vue 3 component using TypeScript that fetches a list of "users" from a mock API endpoint. The data fetching process should occur asynchronously within the setup function.
Key Requirements:
- Asynchronous Data Fetching: Implement a mechanism to fetch data from
https://jsonplaceholder.typicode.com/userswhen the component is mounted. - Loading State: Display a "Loading..." message to the user while the data is being fetched.
- Error Handling: If an error occurs during the data fetching process, display an informative error message to the user.
- Data Display: Once the data is successfully fetched, display a list of user names.
- Reactivity: Ensure that the loading state, error message, and fetched data are reactive and update the UI accordingly.
Expected Behavior:
- When the component is first rendered, the UI should show "Loading...".
- If the API call is successful, the "Loading..." message should disappear, and a list of user names (e.g., "John Doe", "Jane Smith") should be displayed.
- If the API call fails (e.g., network error, invalid URL), the "Loading..." message should disappear, and an error message like "Error fetching users." should be displayed.
Edge Cases:
- Consider what happens if the API returns an empty array of users. The component should gracefully handle this by displaying a message like "No users found." instead of an empty list.
Examples
Example 1:
Scenario: Successful data fetch.
Input: (Implicitly, the component is mounted and calls the API)
Output (rendered in UI):
Loading...
(After a short delay)
User List:
- Leanne Graham
- Ervin Howell
- Clementine Bauch
- ... (rest of the user names)
Explanation: The component initiates the fetch, displays "Loading...", and upon successful retrieval of user data from the API, it updates the UI to show the list of names.
Example 2:
Scenario: API request fails.
Input: (Implicitly, the component is mounted and calls the API, but the API returns an error or is unreachable)
Output (rendered in UI):
Loading...
(After a short delay)
Error fetching users.
Explanation: The component attempts to fetch data, shows "Loading...", but encounters an error. It then displays an error message.
Example 3:
Scenario: API returns an empty user list.
Input: (Implicitly, the component is mounted and calls the API, and the API returns [])
Output (rendered in UI):
Loading...
(After a short delay)
No users found.
Explanation: The API call succeeds but returns no user data. The component handles this by displaying a "No users found" message.
Constraints
- The API endpoint
https://jsonplaceholder.typicode.com/usersmust be used. - The solution must be implemented using Vue 3 and TypeScript.
- The
setupfunction should be used for data fetching and state management. - Avoid using external state management libraries like Pinia or Vuex for this challenge.
- The solution should be efficient and not perform unnecessary re-renders.
Notes
- You can use
fetchAPI or a library likeaxiosfor making HTTP requests. Choose one and stick with it for consistency. - The
onMountedlifecycle hook is a good place to initiate asynchronous operations insetup. - Remember to handle loading and error states reactively using
reforreactivefrom Vue. - Consider how you will manage the asynchronous nature of the fetch operation to avoid race conditions or updating the UI with stale data.
- Think about how to represent the user data (e.g., define an interface for a
Userobject).