Last active
July 11, 2020 15:49
-
-
Save yuripramos/0b4aed91f5165ec838fd6c4522722e0e to your computer and use it in GitHub Desktop.
useFetch custom hook
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 { 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