You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
20 lines
526 B
TypeScript
20 lines
526 B
TypeScript
import { useEffect, useState } from 'react';
|
|
|
|
export function useNetwork() {
|
|
const [isOnline, setNetwork] = useState(window.navigator.onLine);
|
|
const updateNetwork = () => {
|
|
setNetwork(window.navigator.onLine);
|
|
};
|
|
|
|
useEffect(() => {
|
|
window.addEventListener('offline', updateNetwork);
|
|
window.addEventListener('online', updateNetwork);
|
|
|
|
return () => {
|
|
window.removeEventListener('offline', updateNetwork);
|
|
window.removeEventListener('online', updateNetwork);
|
|
};
|
|
});
|
|
return isOnline;
|
|
}
|