Hone logo
Hone
Problems

Asynchronous Data Fetching and Setup in Vue 3 with TypeScript

Vue 3's setup function provides a powerful way to manage component logic and data. This challenge focuses on implementing asynchronous data fetching within the setup function, ensuring proper loading states and error handling for a more robust and user-friendly Vue component. You'll be building a component that fetches data from a mock API and displays it, handling loading and error states gracefully.

Problem Description

You need to create a Vue 3 component using TypeScript that fetches data from a mock API endpoint and displays it. The component should:

  1. Fetch Data: Use async/await to fetch data from https://jsonplaceholder.typicode.com/todos/1.
  2. Loading State: Display a loading indicator while the data is being fetched.
  3. Error Handling: Display an error message if the data fetching fails.
  4. Data Display: Once the data is successfully fetched, display the title property of the fetched todo item.
  5. Reactive Data: Use ref from Vue to manage the loading state, error state, and the fetched data, ensuring reactivity.

Key Requirements:

  • The component must be written in TypeScript.
  • The setup function must be used.
  • ref must be used to manage reactive state.
  • Proper error handling must be implemented.
  • The component should display a loading indicator while fetching data.
  • The component should display an error message if the fetch fails.
  • The component should display the title of the fetched todo item upon successful fetch.

Expected Behavior:

  • Initially, the component should display a loading indicator.
  • After a short delay (simulating network latency), the component should either display the todo title or an error message.
  • If the fetch is successful, the component should display "Todo Title: [title]".
  • If the fetch fails (e.g., due to a network error), the component should display "Error: [error message]".

Edge Cases to Consider:

  • Network errors (e.g., the API endpoint is unavailable).
  • Unexpected data format from the API (although this is unlikely with the provided endpoint).
  • Handling potential race conditions if the component is re-rendered while the fetch is in progress (though this is less critical for this specific problem).

Examples

Example 1:

Input:  A Vue component initialized with no data.
Output: Initially displays a loading indicator. After approximately 1 second, displays "Todo Title: delectus aut autem"
Explanation: The component successfully fetches the data and displays the title.

Example 2:

Input:  Simulate a network error (e.g., by mocking the fetch function to throw an error).
Output: Initially displays a loading indicator. After approximately 1 second, displays "Error: Failed to fetch data."
Explanation: The component handles the network error and displays an appropriate error message.

Constraints

  • API Endpoint: The API endpoint is fixed: https://jsonplaceholder.typicode.com/todos/1.
  • Data Format: Assume the API returns a JSON object with a title property (string).
  • Time Limit: The solution should be reasonably efficient. While performance isn't a primary concern, avoid unnecessary computations.
  • Framework: Vue 3 with TypeScript. Do not use Vue 2 or other frameworks.

Notes

  • Consider using a try/catch block to handle potential errors during the data fetching process.
  • The loading indicator can be a simple text element or a more sophisticated UI element.
  • The error message should be informative and user-friendly.
  • Focus on demonstrating the correct use of async/await, ref, and error handling within the setup function.
  • You can use a simple <div> element to display the loading indicator and error message. Styling is not required for this challenge.
Loading editor...
typescript