I have a helper function checkOnline, with a function which tests whether my app is online or offline. I then have a component with a modal. I want to update the state of this modal depending on whether the user is online or not. If he is offline I want to make the modal disapear. I am not sure on how to call the function, if the eventlistenner is taken into account. I also do not want to call the function on repeat. Maybe I have to put it in a useEffect. The goal is to run the function depending on wether the addeventlistenner changes from online to offline...
checkOnline.js
export const isOnline = async () => {
if (!window.navigator.onLine) return false;
// avoid CORS errors with a request to your own origin
const url = new URL(window.location.origin);
// random value to prevent cached responses
url.searchParams.set('rand', Date.now());
try {
const response = await fetch(url.toString(), { method: 'HEAD' });
return response.ok;
} catch {
return false;
}
};
window.addEventListener('online', () => console.log('online'));
window.addEventListener('offline', () => console.log('offline'));
App.js
import { isOnline } from "../../helpers/CheckOnline";
export default function App() {
const [isModalVisible, setIsModalVisible] = useState(false);
const modal = async () => {
const online = await isOnline();
if (!online) {
setIsModalVisible(false);
}
};
modal();
return (
<div className="App">
<Modal centered visible={isModalVisible}>
<p>test</p>
</Modal>
</div>
);
}