@jsnooks/use-network
v1.0.1
Published
React Hook to listen when the user goes online or offline.
Downloads
1
Maintainers
Readme
useNetwork
navigator
가 online/offline 되는 것을 막아주는 hook
[실습예제]
import React, { useEffect, useState } from 'react';
const useNetwork = (onChange) => {
const [status, setStatus] = useState(navigator.onLine);
const handleChange = () => {
if (typeof onChange === 'function') {
onChange(navigator.onLine);
}
setStatus(navigator.onLine);
};
useEffect(() => {
window.addEventListener('online', handleChange);
window.addEventListener('offline', handleChange);
return () => {
window.removeEventListener('online', handleChange);
window.removeEventListener('offline', handleChange);
};
}, []);
return status;
};
const App = () => {
const handleNetworkChange = (online) => {
console.log(online ? 'We just went online' : 'We are offline');
};
const onLine = useNetwork(handleNetworkChange);
return (
<div className='App'>
<h1>{onLine ? 'Online' : 'Offline'}</h1>
</div>
);
};
export default App;