7d6c91c15a
Was trying to save one event listener in the previous implementation and rely on other redux state-changes to spark a GUI update, but turns out there are scenarios where nothing is updated and the "offline" nag is stuck on screen. ## Change Do it properly using the event listeners. The nag should now update promptly.
25 lines
593 B
JavaScript
25 lines
593 B
JavaScript
import React from 'react';
|
|
|
|
export default function useConnectionStatus() {
|
|
const [online, setOnline] = React.useState(window.navigator.onLine);
|
|
|
|
React.useEffect(() => {
|
|
function handleOnline(event) {
|
|
setOnline(true);
|
|
}
|
|
|
|
function handleOffline(event) {
|
|
setOnline(false);
|
|
}
|
|
|
|
window.addEventListener('online', handleOnline);
|
|
window.addEventListener('offline', handleOffline);
|
|
|
|
return () => {
|
|
window.removeEventListener('online', handleOnline);
|
|
window.removeEventListener('offline', handleOffline);
|
|
};
|
|
}, []);
|
|
|
|
return { online };
|
|
}
|