Skip to content
Rung

07/12

No. 07Medium20 min

Divide & Conquer: Custom Hooks

Mission

Separate business logic from the UI by extracting data fetching into a reusable, generic Custom Hook.

Tasks

  1. 1

    Open hooks/useFetch.js and move the state (data, loading, error) and the useEffect into it.

  2. 2

    Make the Hook generic: it receives a url parameter.

  3. 3

    Return an object { data, loading, error }.

  4. 4

    Refactor App.js to call useFetch and remove every trace of fetching from the component.

  5. 5

    Render the loading state, the error, and the list of products.