Skip to content

Instantly share code, notes, and snippets.

@ruliarmando
Created July 10, 2019 10:15
Show Gist options
  • Save ruliarmando/68020a20c8c0ef4b2ecdc525e86af097 to your computer and use it in GitHub Desktop.
Save ruliarmando/68020a20c8c0ef4b2ecdc525e86af097 to your computer and use it in GitHub Desktop.
import { useState, useEffect } from 'react';
import api from 'utils/api';
const useFecth = (url, params) => {
const [response, setResponse] = useState(null);
const [error, setError] = useState(null);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
const fetchData = async () => {
setIsLoading(true);
try {
const res = await api.get(url, { params });
setResponse(res);
setIsLoading(false);
} catch (err) {
setError(err);
setIsLoading(false);
}
};
fetchData();
}, [url, params]);
return { response, isLoading, error };
};
export default useFecth;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment