Last active
November 12, 2018 03:25
-
-
Save Keldrik/df34a19a46e7586181cb0a2e4e97fa43 to your computer and use it in GitHub Desktop.
React Hooks - Using useState and useEffect for a simple generic data fetch
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"; | |
const useFetch = url => { | |
const [data, setData] = useState(null); | |
const [loading, setLoading] = useState(true); | |
useEffect(async () => { | |
const response = await fetch(url); | |
const data = await response.json(); | |
const [item] = data.results; | |
setData(item); | |
setLoading(false); | |
}, []); | |
return { data, loading }; | |
}; | |
export default useFetch; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment