Created
October 21, 2019 17:07
-
-
Save luanlmd/03305e80b8923e221209a92dd2f0be4b to your computer and use it in GitHub Desktop.
This file contains 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 { useEffect, useState } from 'react'; | |
export const useFetch = (url: string) => { | |
const [data, setData] = useState(''); | |
const [loading, setLoading] = useState(false); | |
useEffect(() => { | |
if (url !== '') { | |
setData(''); | |
setLoading(true); | |
fetch( | |
url, | |
{ headers: { Authorization: '3fe0e403f588db5d1d734c743b7818eaa4b96ca98df74e9802278975a7de4942' } }) | |
.then(x => x.text()) | |
.then(y => { | |
setData(y); | |
setLoading(false); | |
} | |
); | |
} | |
}, [url]); | |
return { data, loading }; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment