I created this function in my React app to fetch a url
and check the status.
import { useEffect, useState } from 'react';
const fetchStatusCode = (url: string) => {
const [data, setData] = useState<string | null>(null);
useEffect(() => {
const fetchStatus = async () => {
const response = await fetch(url);
return response.statusText;
};
fetchStatus().then((res) => setData(res));
}, []);
return data;
};
export default fetchStatusCode;
I can then use it like this in a component:
console.log('Status', fetchStatusCode('http://my-site.com/abc/token'));
In the browser console I get:
Status null
Status null
Status Ok
Is that the intended behaviour? Why get multiple console log outputs (first 2 times null
and then OK
)?