Was this helpful?
import React from "react"
import { useAsync } from "react-async"
const fetchPerson = async ({ id }, { signal }) => {
const response = await fetch(`https://swapi.co/api/people/${id}/`, { signal })
if (!response.ok) throw new Error(response.status)
return response.json()
}
const Person = ({ id }) => {
const { data, error } = useAsync({ promiseFn: fetchPerson, id })
if (error) return error.message
if (data) return `Hi, my name is ${data.name}!`
return null
}
const App = () => {
return <Person id={1} />
}