use-fetch-react
v0.0.3
Published
Using fetch in a react hook.
Downloads
3
Readme
use-fetch-react
Usage
import { useFetch } from "use-fetch-react"
const Page = () => {
const [request, { data, error, loading, called }] = useFetch(
"https://swapi.co/api/people",
{
onCompleted(response) {
console.log("onCompleted", response)
},
onError(err) {
console.log("onError", err)
},
}
)
return (
<>
{!called && <p>Please press the fetch button</p>}
{error && <ErrorMessage>{error.message}</ErrorMessage>}
<Button
loading={loading}
onClick={request}
>
Request
</Button>
{loading && (
<div>
<BarLoader />
</div>
)}
{data &&
data.results &&
data.results.map((person, index) => {
return <div key={index}>{person.name}</div>
})}
</>
)
}