Last active
May 22, 2021 03:03
-
-
Save karpolan/59e65f8bab84349b967125b2b280d23a to your computer and use it in GitHub Desktop.
useAxios() hook with single object as axios's params
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
export const useAxios = (axiosParams) => { | |
const [response, setResponse] = useState(undefined); | |
const [error, setError] = useState(''); | |
const [loading, setloading] = useState(true); | |
const fetchData = async (params) => { | |
try { | |
const result = await axios.request(params); | |
setResponse(result.data); | |
} catch (error) { | |
setError(error); | |
} finally { | |
setLoading(false); | |
} | |
}; | |
useEffect(() => { | |
fetchData(axiosParams); | |
}, []); // execute one time | |
return { response, error, loading }; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment