Skip to content

Instantly share code, notes, and snippets.

@alexaleluia12
Created August 7, 2023 00:01
Show Gist options
  • Save alexaleluia12/998fb84abacb62a3bcee580dd30fb672 to your computer and use it in GitHub Desktop.
Save alexaleluia12/998fb84abacb62a3bcee580dd30fb672 to your computer and use it in GitHub Desktop.
Custon Hook React
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