You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
21 lines
521 B
JavaScript
21 lines
521 B
JavaScript
3 years ago
|
import { useEffect, useState } from 'react'
|
||
|
import axios from 'axios'
|
||
|
|
||
|
const useApi = (url, mapResults = (result) => result) => {
|
||
|
const [data, setData] = useState()
|
||
|
const [isLoading, setIsLoading] = useState(true)
|
||
|
const [error, setError] = useState()
|
||
|
useEffect(() => {
|
||
|
setIsLoading(true)
|
||
|
axios
|
||
|
.get(url)
|
||
|
.then(response => setData(mapResults(response.data)))
|
||
|
.catch(setError)
|
||
|
.finally(() => setIsLoading(false))
|
||
|
}, [url])
|
||
|
|
||
|
return { data, isLoading, error }
|
||
|
}
|
||
|
|
||
|
export { useApi }
|