Skip to content

Instantly share code, notes, and snippets.

@karpolan
Last active May 22, 2021 03:03
Show Gist options
  • Save karpolan/59e65f8bab84349b967125b2b280d23a to your computer and use it in GitHub Desktop.
Save karpolan/59e65f8bab84349b967125b2b280d23a to your computer and use it in GitHub Desktop.
useAxios() hook with single object as axios's params
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