Created
September 26, 2020 08:41
-
-
Save samipshah100/05dcdc937ee60c443de2c40b24ef1a93 to your computer and use it in GitHub Desktop.
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' | |
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