Created
August 7, 2023 00:01
-
-
Save alexaleluia12/998fb84abacb62a3bcee580dd30fb672 to your computer and use it in GitHub Desktop.
Custon Hook React
This file contains hidden or 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 const useFetch = (url) => { | |
const [data, setData] = useState([]) | |
useEffect(() => { | |
const fetchData = async () => { | |
try { | |
const response = await fetch(url) | |
const data = await response.json() | |
setData(data) | |
} catch (error) { | |
console.log(error) | |
} | |
} | |
fetchData() | |
}, [url]) | |
return data | |
} | |
export default useFetch |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment