Rev 6810 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import { useEffect, useState } from 'react'
import { axios } from '../utils'
const useFetchHelper = (helper) => {
const [data, setData] = useState({})
const [loading, setLoading] = useState(false)
const [error, setError] = useState(null)
useEffect(() => {
const getHelper = (helper) => {
setLoading(true)
axios
.get(`/helpers/${helper}`)
.then(({ data: response }) => {
const { data, success } = response
if (!success) {
const error =
typeof data === 'string' ? data : 'Ha ocurrido un error'
setError(error)
return
}
setData(data)
})
.catch((err) => setError(`Error: ${err}`))
.finally(() => setLoading(false))
}
getHelper(helper)
}, [])
return { data, error, loading }
}
export default useFetchHelper