Frontend School

Build a Robust useFetch Hook in React with Loading & Error States

Machine Coding
medium

Build a Robust useFetch Hook in React with Loading & Error States

Medium
React
Hooks
custom-hooks
Data Fetching
Error Handling
loading-states
Machine Coding
frontend-interview
TypeScript

Overview

This problem challenges you to build a custom useFetch React hook that elegantly handles data fetching, loading states, and error management. You will create a reusable hook to fetch data from a public API and display it within a simple UI, demonstrating robust error handling and user feedback. This task is ideal for practicing custom hooks, state management, and UI rendering based on async operations.

What you need to do / Task

  1. Implement a custom React hook named useFetch.
  2. The useFetch hook should accept a URL string as its primary argument.
  3. It must return an object containing data, loading (boolean), error (any), and a refetch function.
  4. Integrate useFetch into /App.tsx to fetch data from a provided public API endpoint.
  5. Display the fetched data (or a relevant subset) in the UI.
  6. Show a clear loading indicator while data is being fetched.
  7. Display an informative error message if the fetch operation fails, along with a "Retry" button.
  8. The "Retry" button should re-attempt the fetch request.

UI/UX expectations

  • A main container for the application content.
  • A prominent loading message or spinner should appear when loading is true.
  • If error is present, display the error message clearly, perhaps in red text, alongside a "Retry" button.
  • Once data is successfully fetched, display it in a simple, readable format (e.g., a list of items).
  • The "Retry" button should be visible only when an error occurs.

Acceptance criteria

  • The useFetch hook is correctly implemented and adheres to React hook rules.
  • Data is successfully fetched and displayed in the UI.
  • A loading indicator is visible during the data fetching process.
  • An error message is displayed when the API request fails (e.g., invalid URL, network error).
  • A "Retry" button is available when an error occurs and successfully re-fetches data.
  • The UI updates correctly when loading, error, or data changes.

Clarifications

  • Q: Which API should I use? A: Use https://jsonplaceholder.typicode.com/posts for fetching a list of posts.
  • Q: Should the hook handle POST/PUT/DELETE requests? A: No, focus solely on GET requests for this problem.
  • Q: How should I simulate an error? A: You can temporarily change the URL to an invalid one (e.g., https://jsonplaceholder.typicode.com/invalid-endpoint) in /App.tsx to test error states.
  • Q: Are external data-fetching libraries allowed? A: No, implement useFetch using native fetch API.

Constraints

Constraints & assumptions

  • Assumed stack: React with TypeScript.
  • Use the native fetch API; no third-party libraries like Axios or React Query for data fetching.
  • The UI should be functional and clear, but extensive styling is not required.
  • The useFetch hook should be generic enough to fetch any JSON data.

Out of scope

  • Advanced caching mechanisms.
  • Authentication or authorization headers.
  • Handling HTTP methods other than GET.
  • Deep UI customization or responsiveness.

What graders evaluate

  • Correct and idiomatic implementation of the useFetch custom hook.
  • Effective management of loading, error, and data states within the hook.
  • Clean integration of the hook into the component, demonstrating its reusability.
  • Robust error handling, including a functional retry mechanism.

Loading editor...