Hooks and Data Fetching Exercise

Build a users page that fetches data from an API.

This tests useState, useEffect, conditional rendering, and UI states.

Requirements

Show:

State design

const [users, setUsers] = useState([])
const [isLoading, setIsLoading] = useState(false)
const [error, setError] = useState(null)
const [search, setSearch] = useState('')

Fetch flow

useEffect(() => {
  async function loadUsers() {
    try {
      setIsLoading(true)
      setError(null)

      const response = await fetch('/api/users')

      if (!response.ok) {
        throw new Error('Failed to load users')
      }

      const result = await response.json()
      setUsers(result.data)
    } catch (error) {
      setError(error.message)
    } finally {
      setIsLoading(false)
    }
  }

  loadUsers()
}, [])

What to explain