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.
		
		
		
		
		
			
		
			
				
	
	
		
			23 lines
		
	
	
		
			744 B
		
	
	
	
		
			TypeScript
		
	
			
		
		
	
	
			23 lines
		
	
	
		
			744 B
		
	
	
	
		
			TypeScript
		
	
import { useEffect, useState } from 'react';
 | 
						|
 | 
						|
export function useNetwork() {
 | 
						|
  const [isOnline, setNetwork] = useState(window.navigator.onLine);
 | 
						|
  const updateNetwork = () => {
 | 
						|
    setNetwork(window.navigator.onLine);
 | 
						|
  };
 | 
						|
 | 
						|
  // there are some weird behavior with this api.
 | 
						|
  // basically, online events might not be called if the pc has a virtual machine running
 | 
						|
  // https://github.com/electron/electron/issues/11290#issuecomment-348598311
 | 
						|
  useEffect(() => {
 | 
						|
    window.addEventListener('offline', updateNetwork);
 | 
						|
    window.addEventListener('online', updateNetwork);
 | 
						|
 | 
						|
    return () => {
 | 
						|
      window.removeEventListener('offline', updateNetwork);
 | 
						|
      window.removeEventListener('online', updateNetwork);
 | 
						|
    };
 | 
						|
  });
 | 
						|
  return isOnline;
 | 
						|
}
 |