Skip to content

Instantly share code, notes, and snippets.

@luanlmd
Created October 21, 2019 17:07
Show Gist options
  • Save luanlmd/03305e80b8923e221209a92dd2f0be4b to your computer and use it in GitHub Desktop.
Save luanlmd/03305e80b8923e221209a92dd2f0be4b to your computer and use it in GitHub Desktop.
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