Skip to content

Instantly share code, notes, and snippets.

@yuripramos
Last active July 11, 2020 15:49
Show Gist options
  • Save yuripramos/0b4aed91f5165ec838fd6c4522722e0e to your computer and use it in GitHub Desktop.
Save yuripramos/0b4aed91f5165ec838fd6c4522722e0e to your computer and use it in GitHub Desktop.
useFetch custom hook
import { useState, useEffect } from 'react';
export function useFetch(
doFetch,
dependencies
) {
const [loading, setLoading] = useState(false);
const [data, setData] = useState();
useEffect(() => {
setLoading(true);
doFetch()
.then(response => {
setData(response);
setLoading(false);
})
.catch(() => {
setData(null);
setLoading(false);
});
}, [...dependencies]);
return { loading, data };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment