Skip to content
Rung

06/12

No. 06Medium15 min

Robust Data Fetching

Mission

Fetch data from an API with useEffect and async/await, handling the three states every request has: loading, error and success.

Tasks

  1. 1

    Declare three states: data, loading (boolean) and error.

  2. 2

    Create an async function inside useEffect that calls fetch.

  3. 3

    Wrap it in try/catch/finally to handle network and API failures.

  4. 4

    Throw when the response is not ok, because fetch does not do it for you.

  5. 5

    Render each state: a loading message, the error message, or the list of results.