Skip to content

Instantly share code, notes, and snippets.

@samipshah100
Created September 26, 2020 08:41
Show Gist options
  • Save samipshah100/05dcdc937ee60c443de2c40b24ef1a93 to your computer and use it in GitHub Desktop.
Save samipshah100/05dcdc937ee60c443de2c40b24ef1a93 to your computer and use it in GitHub Desktop.
import { useState, useEffect } from 'react'
import axios from 'axios'
export const useFetchEffect = (url) => {
const [isLoading, setIsLoading] = useState(false)
const [isError, setIsError] = useState(false)
const [responseData, setResponseData] = useState(null)
useEffect(() => {
const fetchData = async () => {
setIsError(false)
setIsLoading(true)
try {
const result = await axios(url)
setResponseData(result.data)
} catch (error) {
setIsError(true)
}
setIsLoading(false)
}
fetchData()
}, [url])
const dataFromHook = { isLoading, isError, responseData }
return dataFromHook
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment