I have this useFetch custom hook
export function useFetch(initialState, url, header) {
const [value, setValue] = useState(initialState);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
const FetchURL = async () => {
try {
const res = await fetch(url, {
headers: header,
});
if (!res.ok)
throw new Error("There was and error getting data from API");
const data = await res.json();
setValue(data.record);
} catch (error) {
setError(error.message);
console.error(error);
} finally {
setIsLoading(false);
}
};
FetchURL();
}, [header, url]);
return [error, isLoading, value, setError];
}
and i'm using it like this
const [error, isLoading, tabsArr, setError] = useFetch([], url, header);
return (
{error && <ErrorMessage onClick={()=> setError(false)}/> }
)
I want the code to be fetched on mount and then if there is an Error i'm giving an error message with a button, I want it to try to fetch again on click of the button.