Created
July 10, 2019 10:15
-
-
Save ruliarmando/68020a20c8c0ef4b2ecdc525e86af097 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect } from 'react'; | |
import api from 'utils/api'; | |
const useFecth = (url, params) => { | |
const [response, setResponse] = useState(null); | |
const [error, setError] = useState(null); | |
const [isLoading, setIsLoading] = useState(true); | |
useEffect(() => { | |
const fetchData = async () => { | |
setIsLoading(true); | |
try { | |
const res = await api.get(url, { params }); | |
setResponse(res); | |
setIsLoading(false); | |
} catch (err) { | |
setError(err); | |
setIsLoading(false); | |
} | |
}; | |
fetchData(); | |
}, [url, params]); | |
return { response, isLoading, error }; | |
}; | |
export default useFecth; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment